【发布时间】:2019-01-03 13:53:02
【问题描述】:
这是我用来关闭我的应用程序及其相关进程并删除在应用程序使用过程中提取的所有文件的代码:
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);
}
}
但是,问题在于,当它尝试删除文件(即应用中某处使用的图像)时,会显示以下异常:
“该进程无法访问该文件,因为它正被另一个进程使用。”
更新:
我发现进程“Mastersolution.vhost.exe”保存着我试图删除的所有文件。 Mastersolution 实际上是我正在关闭的主应用程序 App.Current.Shutdown(); 所以我需要在删除文件之前以某种方式断开文件与主应用程序的连接。但是要这样做吗?
【问题讨论】:
-
这是 winforms/wpf/console 应用程序吗?杀死进程和gui似乎不是最好的做事方式..
-
这是 wpf。 gui 我的意思是主应用程序