I have a following question. Right now, we are writing out config files at shutdown. It happened that the user got a process kill during writing which resulted in corrupt configuration. Thus I have to make sure that the file on the disk is always correct, and no partially written files etc are there. Of course, I can implement some pattern using backup files myself (like write to backup, then replace the old file with the new one, some logic to detect and recover failed write.... ) but maybe there is a kind of a framework out there which handles this already?
Asked
Active
Viewed 281 times
0
-
you might have a look at the transactional file system that has been available since Vista. You'll need PInvoke to use it. An alternative is using a database instead of a file – Emond Apr 04 '13 at 12:04
2 Answers
1
Transactional NTFS, introduced with Windows Vista, allows to execute atomic operations on the file system (read, write, delete...).
Here's a managed wrapper you could use for .Net: http://archive.msdn.microsoft.com/txfmanaged
Do note that, sadly, Microsoft is considering deprecating this great feature of Windows. See http://msdn.microsoft.com/en-us/library/windows/desktop/hh802690(v=vs.85).aspx

ken2k
- 48,145
- 10
- 116
- 176
0
You could write the configuration file to a temporary file and then replace the actual file with the temporary file by using the Sysyem.IO.File.Replace
method:
string config = @"C:\my_config.xml";
string backup = @"C:\my_config.bak";
string temp = @"C:\temp_config.xml";
SaveConfigurationTo(temp);
File.Replace(temp, config, backup);
If you don't want to create a backup of the old configuration file, you can pass null
as third argument.
See: File.Replace Method on MSDN.

Olivier Jacot-Descombes
- 104,806
- 13
- 138
- 188