Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 762
Martin Ba Avatar asked Dec 18 '25 04:12

Martin Ba


1 Answers

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.

like image 58
Steve Townsend Avatar answered Dec 20 '25 20:12

Steve Townsend