Here is the code that I use to close my application, its associated processes and to delete all the files that have been extracted during the use of 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 it tries to delete the files (which are the images used somewhere in the app) it shows the following exception:
"The process cannot access the file because it is being used by another process."
Update:
I found that the process 'Mastersolution.vhost.exe' is holding all the files that I am attempting to delete. Mastersolution is actually the main app that I am closing on the line App.Current.Shutdown();
So I need to somehow disconnect the files from the main application prior to deleting them. But hoe to do this?