Why does throwing an exception from the OnDeleteFile callback/event handler not prevent file deletion?

This is the correct behavior. Windows can delete a file or directory in two ways:

The first way:

  1. The file is opened.
  2. The "Delete on close" flag is set for the file (for details, see the flag FileDispositionInformation in MSDN). During this system request, the OnCanFileBeDeleted callback is called.
  3. The file is closed. This causes CBFS Connect to call the OnDeleteFile callback.

The second way (Explorer in Windows 8 uses this, but any application can go this way and some applications do):

  1. The file is opened with the special system "delete on close" flag (for details see FILE_FLAG_DELETE_ON_CLOSE in the CreateFile Win32 API).
  2. The file is closed. CBFS Connect calls the OnCanFileBeDeleted callback and if the callback handler allows the deletion of the file, the OnDeleteFile callback is called (in the other case only the OnClose callback is called).

Unfortunately, the system doesn't check the status of the closed file request. It's expected to always finish successfully. That's why throwing exceptions from OnDeleteFile callbacks doesn't work.

But the deletion can be prevented if the OnCanFileBeDeleted callback handler denies it.

In order to protect the file from being deleted in the second variant described above, you need to do the following:

  1. Set the CBFSConnect.UseFileCreationFlags property.
  2. Check for the FILE_FLAG_DELETE_ON_CLOSE flag in the OnOpenFile callback. If it’s present, then throwing the ERROR_ACCESS_DENIED (5) error causes the file not to be opened (and therefore not to be deleted on close).

Unfortunately, such deletion protection can cause some applications or processes to work incorrectly. Consider a process that uses some temporary file(s). The process opens the file, reads/writes it, and closes the handle many times. Finally, the process decides to open the file with the FILE_FLAG_DELETE_ON_CLOSE flag and perform more read/write operations before deleting the file. Preventing the file opening in this last situation can cause an incorrect behavior of the process.

Ready to get started?

Learn more about Callback Technologies or download a free trial.

Download Now