How do you persist data to disk from .NET? How do you persist data to disk from .NET? database database

How do you persist data to disk from .NET?


Some of the constructors for .NET's FileStream class take a parameter of type FileOptions. One of the values for FileOptions is WriteThrough, which "Indicates that the system should write through any intermediate cache and go directly to disk."

This should ensure that by the time your write operation (to a new file) returns, the data is committed to disk and you can safely delete the old file.


This can be done via Serialization.

The .NET Framework includes many built-in options for serializing your data to disk, including using binary or XML-based formats. Detailed How-To articles are provided in the MSDN Documentation.


In order to do this, you will require a resource which will allow you to engage in a Transaction (more often than not, you would use a TransactionScope.

Most databases will participate in a Transaction if one is contained. Disk operations can also be managed by a Transaction, but you would have to do some specific work in order to utilize it in .NET.

Also, note that this is only available on Windows Vista and later.

If you go the database route, then you could store the serialized contents of your trees in a blob (or text, depending on the serialization mechanism).

Note, you can also use the FILESTREAM functionality in SQL Server (2008 and up, I believe) to store your files on the filesystem and gain the benefits of transactions in SQL Server.