Serialization has nothing to do with this. Your actual question is: how do I write a bunch of data atomically to disk? Here are a few options:
- If the data is small enough to fit into a single disk cluster (512 bytes or 4K) use
FILE_FLAG_WRITE_THROUGH | FILE_FLAG_NO_BUFFERING
to atomically write a single disk sector in a single write call. - Use transactional NTFS to atomically write any amount of data.
- Write to a
.tmp
file and atomically rename to the wanted file name - Use some other means to achieve atomicity like databases (maybe even SqlLite, Esent, ...) or the CLFS of Windows.