How to close a file that is already open in a directory

I am trying to parse a folder and delete all files in it.

DirectoryInfo dir = new DirectoryInfo("C\\Temp"); if (dir.GetDirectories().Any(p => p.Name == "\\NewTemp")) { foreach (string file in Directory.GetFiles(dir + "\\NewTemp")) { File.SetAttributes(file, FileAttributes.Normal); File.Delete(file); } } 

This code works fine and deletes all files in the \ NewTemp folder. But if any of the files is open, these files will not be deleted. I want to block the files that open and delete them. I even tried

 foreach (string file in Directory.GetFiles(dir + "\\NewTemp")) { TextReader tr = new StreamReader(dir+"\\NewTemp\\"+file); tr.Close(); File.SetAttributes(file, FileAttributes.Normal); File.Delete(file); } 

But to no avail. Please let me know where I am missing.

+4
source share
5 answers

Please let me know where I am missing.

Well, you close your file descriptor - but that will not affect any other file descriptors, whether they belong to your process or not. In principle, you cannot delete a file that is used elsewhere, unless it has been explicitly opened with a file sharing flag that allows you to delete it. As far as I know, this is how Windows file systems work. (EDIT: Initially, I suggested using Notepad as a way to keep the file descriptor open, but apparently it closes the descriptor after loading it into memory. Um, try other applications :)

+5
source

You cannot delete a file if the handle is opened by another process. You will have to delete this process first before you can delete the file.

You can look at this example to find out how to find all open descriptors.

+1
source

This is a convenient tool for finding file processing:

http://technet.microsoft.com/en-us/sysinternals/bb896653.aspx

0
source

If you know which program is open for your file, you can simply taskkill this program directly and then delete your file.

If you don’t know which process is open for your file, you can use such a utility and then process the output. I redirected the output myself and then used regex to find which process I want to get, but probably the best way .

Keep in mind that this method is not completely safe and can kill processes that you do not want to kill.

0
source

These are your options, in the order in which you should try them (from the least invasive to the very invasive).

  • Do what you are doing now (delete the read-only attribute and try deleting the file).
  • You can try renaming the file and then deleting it.
  • If this does not work, you should open the file with the FILE_FLAG_DELETE_ON_CLOSE flag (output to CreateFile ). When all other file descriptors are closed, the system will delete the file for you.
  • If the above CreateFile call failed because the file cannot be opened with FILE_SHARE_DELETE , you must call MoveFileEx with MOVEFILE_DELAY_UNTIL_REBOOT (and lpNewFileName is set to NULL / IntPtr.Zero). This will delete the file the next time you reboot / reboot the system.
  • If step 4 is not practical (that is, you cannot wait for a reboot), then you should continue the path to kill random processes that may lock the file. Just keep in mind that this can lead to system instability, especially if it is the system process that you are trying to kill.
0
source

Source: https://habr.com/ru/post/1437826/


All Articles