1

I have a file that was opened with with the Windows API CreateFile() using the FILE_SHARE_DELETE flag so that I can delete it while the file handle is still open. But in the window between the first process deleting the file and the process ending, I want to be able to tell that the file is being deleted so that I can go into a retry loop.

I found error code 303 that looks exactly like what I'm looking for:

ERROR_DELETE_PENDING
    303 (0x12F)
    The file cannot be opened because it is in the process of being deleted.

But I can't find what I can use that will return this code for me. Does anyone know how I can determine that my file is in this state of being deleted but still has an open handle?

Takehiko
  • 83
  • 1
  • 7

2 Answers2

4

On Vista and later, you can open the file with CreateFile() (make sure the FILE_SHARE_DELETE flag is specified so the open can succeed) and then use GetFileInformationByHandleEx() to retrieve the file's FILE_STANDARD_INFO structure. It has a BOOLEAN DeletePending data member that will be TRUE if the file has been marked for deletion.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
  • On pre-Vista you can use [`NtQueryInformationFile`](http://undocumented.ntinternals.net/UserMode/Undocumented%20Functions/NT%20Objects/File/NtQueryInformationFile.html) (`GetFileInformationByHandleEx` is basically a wrapper around it). – Igor Skochinsky Aug 06 '13 at 10:29
  • After the first process deletes the file, the second process can no longer open a file handle on it. `CreateFile()` fails with `ERROR_ACCESS_DENIED`. – Takehiko Aug 06 '13 at 15:04
1

To get that error code you need to call GetLastError immediately after the CreateFile function fails. It will return ERROR_DELETE_PENDING (the constant for error code 303) when that situation occurs

JaredPar
  • 733,204
  • 149
  • 1,241
  • 1,454