Delete on close files

xoreax picture xoreax · Feb 13, 2009 · Viewed 13.9k times · Source

Language used: C#

Theory: I want to create a file with the flag FileOptions.DeleteOnClose in a temporary folder. The file is successfully created and I write dato onto it, the next step is to launch the application associated with the file Process.Start(...) and allow the user to inspect the document, finally I close my handle and as soon as other process close the handle to the temporary file, the file is deleted by operating system.

My problem is that other processes cannot open the file, even for reading, despite if I add FileShare.ReadWrite | FileShare.Delete to the sharing mode.

Any suggestions?

Answer

Duncan Smart picture Duncan Smart · Feb 13, 2009

The other processes need to specify FileShare.Delete when they open the DeleteOnClose file

From the MSDN CreateFile docs:

"FILE_FLAG_DELETE_ON_CLOSE... Subsequent open requests for the file fail, unless the FILE_SHARE_DELETE share mode is specified."