I have an application where I am trying to implement a “safe file overwrite” by:
- Creating a file (
A) - Filling
Awith data - Copying
Ato its final destination (B) viaSHFileOperation - Deleting
AusingDeleteFile
However in step 4 DeleteFile always returns ERROR_SHARING_VIOLATION. The entire process takes milliseconds, so I can’t imagine who else would be interfering with my file. A couple questions:
- Is there a better Win32 (C/C++) technique for performing the above?
- How can I get more information about the “other process” that’s keeping me from deleting file
A? - How do I gently (wink wink nudge nudge) force Windows to delete my temporary file?
Any other suggestions are welcome
My best guess would be that you need a step 2.5) Close the file handle created in 1)
What are you using to create/open the file?
If you use CreateFile then make sure you are closing your file handle before the call to delete, or else make sure you specify the sharing flag
FILE_SHARE_DELETE.You might also want to simplify your copying of file code by using the Win32 API CopyFile.