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?
source share