Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delete on close files

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?

like image 835
xoreax Avatar asked Feb 13 '09 14:02

xoreax


People also ask

How do you delete a file when it says it is open?

Force delete using Windows With the command prompt open, enter del /f filename , where filename is the name of the file or files (you can specify multiple files using commas) you want to delete.

How do I close a file to delete it?

But don't worry, you can restart File Explorer again and get everything back. To close File Explorer, press Ctrl + Shift + ESC, find Windows Explorer, right-click the process, and select End task. Return to the Command Prompt. If you lost the window, press Alt + Tab to jump to it.

How do you delete running files on Windows 10?

Locate the file that you want to delete. Right-click the file, then click Delete on the shortcut menu. Tip: You can also select more than one file to be deleted at the same time. Press and hold the CTRL key as you select multiple files to delete.


1 Answers

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."

like image 185
Duncan Smart Avatar answered Sep 29 '22 21:09

Duncan Smart