I am trying to generate a huge text file using C# and another process is constantly looking at the location and trying to pickup the file if available.
In order to make the file atomic below are the steps :
1 - Write to file : Filename_temp.txt
2 - Check if Filename.txt already exists then Delete
3 - Do a File.Move to the same destination
From filename : Filename_temp.txt
TO : Filename.txt
Since C# does not have a rename, I have to rely on File.Move, does this make sure the move operation will be atomic or is there another way to achieve this atomicity?
'Frequently asked question: Is MoveFileEx atomic if the existing and new files are both on the same drive? The simple answer is "usually, but in some cases it will silently fall-back to a non-atomic method, so don't count on it". '
Atomic renameThe rename function from the C library in Windows does not implement the POSIX atomic behaviour; instead it fails if the destination file already exists. However, other calls in the Windows API do implement the atomic behaviour.
There is no way to do this; file copy operations are never atomic and there is no way to make them. But you can write the file under a random, temporary name and then rename it. Rename operations have to be atomic. If the file already exists, the rename will fail and you'll get an error.
Atomic coordinate files are the data files that specify three-dimensional (3D) molecular structures. At a minimum, they must specify the positions of each atom in space, typically with X, Y and Z Cartesian coordinates, and the chemical element each atom represents.
According to the MSDN blog article How to do atomic writes in a file, renaming an NTFS file is an atomic operation:
The solution? Let's remember that metadata changes are atomic. Rename is such a case. So, we can just perform the write to a temporary file, and after we know that the writes are on the disk (completed and flushed) then we can interchange the old file with the new file.
Granted, this does not guarantee that File.Move
just issues an NTFS rename operation, but I can't think of a valid reason why it should do anything more complicated.
File.Move should be a 'rename' if the source and destination are on the same volume. So, regardless of file size, the Move should be 'instant'. I presume that is your concern?
From a FAQ from a MS employee on http://msdn.microsoft.com/en-gb/library/windows/desktop/aa365240%28v=vs.85%29.aspx we have;
'Frequently asked question: Is MoveFileEx atomic if the existing and new files are both on the same drive?
The simple answer is "usually, but in some cases it will silently fall-back to a non-atomic method, so don't count on it".'
I guess if it's 100% critical you could look at Transactional NTFS. I'm not sure if there are wrappers in .Net for this yet, so you may need to use P/Invoke.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With