How to delete files programmatically?

Here is the code that I use to close my application, its related processes and delete all files that were extracted while using the application:

private void Quit_Click(object sender, RoutedEventArgs e) //close the application { //kill cinector after all import is done Process[] processes = Process.GetProcesses(); for (int i = 0; i < processes.Count(); i++) { if (processes[i].ProcessName.ToLower().Contains("CinectorProcess")) { processes[i].Kill(); } } //also kill powerpoint just in case for (int i = 0; i < processes.Count(); i++) { if (processes[i].ProcessName.ToLower().Contains("powerpnt")) { processes[i].Kill(); } } //kill the engine ShutdownEngine(); //kill the main app App.Current.Shutdown(); //also delete all three folders //slides_png_prev if (Directory.Exists(slides_png_prev)) { Thumbnails = null; Directory.Delete(slides_png_prev, true); } //slides_png if (Directory.Exists(slides_png)) { Directory.Delete(slides_png, true); } //slides_png_prev_seleect if (Directory.Exists(slides_png_prev_seleect)) { Directory.Delete(slides_png_prev_seleect, true); } } 

However, the problem is that when you try to delete files (which are used for images used somewhere in the application), this indicates the following exception:

"The process cannot access the file because it is being used by another process."

Update: I found that the "Mastersolution.vhost.exe" process contains all the files that I am trying to delete. Mastersolution is actually the main application that I am closing on the App.Current.Shutdown(); Therefore, I need to somehow disconnect files from the main application before deleting them. But a hoe for that?

+5
source share
3 answers

Store the files in the temp directory and let the operating system handle it.

See Path.GetTempPath and Path.GetTempFileName .

+3
source

First you need to determine which file in the folder is locked - instead of deleting the entire folder, delete individual files. Then check Process Explorer and Ctrl + F which process is actually using the recoverable file.

You can also use the WhoIsLocking method in this code.

https://bitbucket.org/snippets/wiip/nnGX

You can use it to determine which process is blocking the file. Then you can write something like this to automatically kill this process:

  List<Process> lp = FileUtil.WhoIsLocking(fileName); foreach (Process p in lp) { p.Kill(); } 

This is the last workaround when there are no better options, such as closing a file in your application, closing Powerpoint using automation, etc.

+1
source

You can delete folders in this way without problems if they are not used, and you have permission to do so. You can use the SysInternals Handle to determine which processes are accessing the file / folder you want. Please note: if you cannot delete the files, and not your process accessing them, just raise the warning: “Failed to clear the folder: slide preview” Instead of accidentally killing applications for users until you can delete png.

On the side of the note: Remind me not to use an application that can kill the office and discard any work that I have done.

You must use the pid of each process that you start, or better yet, use IPC to send a friendly shutdown request that can clear resources before closing. For powerpoint, it's as simple as calling Quit() on an application object.

0
source

All Articles