Is it possible to delete a file that is opened by a process under windows?
For testing and simulation purposes I would like to delete a file that is currently open by my process.
The CreateFile docs state that it is possible to open a file in a mode (FILE_SHARE_DELETE) that allows the file the open handle is pointing to to be deleted by another operation. (And I already tried and confirmed this via CreateFile(FILE_SHARE_DELETE) + DeleteFile.)
What I would like to know now however is, whether it is possible at all that a file that is opened by anyone without above mentioned flag to be deleted somehow?
As far as I understand the DeleteFile docs it would not be possible with this functions, as
The DeleteFile function fails if an application attempts to delete a file that is open for normal I/O or as a memory-mapped file.
Is there any other way to delete a file that is open, but does not have the FILE_SHARE_DELETE flag set?
There is no way to do this.
The closest way to do something like this is to schedule delete on reboot using MoveFileEx with a target filename of NULL, and MOVEFILE_DELAY_UNTIL_REBOOT in the dwFlags parameter.