I am looking for how to do a simple transactional POSIX-style rename()
in .net:
If the link named by the new argument exists, it shall be removed and old renamed to new. In this case, a link named new shall remain visible to other processes throughout the renaming operation and refer either to the file referred to by new or old before the operation began.
I shouldn’t have to delete the destination file before renaming the new file which is to replace it. Deleting and then renaming means that the new name could return “file not found” for a nonzero amount of time, which is even worse if the process or machine crashes at that point.
The point is to implement the simple transactional file update pattern where I only want to replace the file if I can successfully write a replacement file to disk first. I’m really hoping that support for such a basic filesystem access operation does not require pinvoke or anything fancy. I should be able to:
- Write a new file.
- Close its
FileStream
which flushes it. - Rename that file over the old file (with the assumption that the FS will, with help of its journal, guarantee that this step is not persisted until the file is actually flushed to hardware and that this rename operation itself is atomic).
Where is this rename functionality hiding in .net’s BCL? Is it not there? If it’s not there, is it because .net was written when Windows 9x was still a supported environment? Or does modern Windows still not support such operations for unprivileged processes?