C# 如何强制关闭WINWORD进程

时间:2023-03-08 20:41:21
C# 如何强制关闭WINWORD进程

private void KillProcess(string processName) //调用方法,传参
{
try
{

Process[] thisproc = Process.GetProcessesByName(processName);
//thisproc.lendth:名字为进程总数

if (thisproc.Length > 0 )

{
for (int i=0; i< thisproc.Length;i++)
{
if (!thisproc[i].CloseMainWindow()) //尝试关闭进程 释放资源
{
thisproc[i].Kill(); //强制关闭

}
Console.WriteLine("进程 {0}关闭成功", processName);
}
}
else
{
Console.WriteLine("进程 {0} 关闭失败!", processName);
}
}
catch //出现异常,表明 kill 进程失败
{
Console.WriteLine(“结束进程{0}出错!", processName);
}C# 如何强制关闭WINWORD进程