public void KillProgress(string progressname)
{
try
{
System.Diagnostics.Process[] myProcesses = System.Diagnostics.Process.GetProcesses();
foreach (System.Diagnostics.Process myProcess in myProcesses)
{
if (progressname == myProcess.ProcessName && myProcess.MainWindowTitle=="")
{
myProcess.Kill();
}
}
}
catch (Exception ex)
{
MessageBox.Show("Delete Excel Process Error:" + ex.Message);
}
}
//调用方法
KillProgress("ABC");
解释:
myProcess.MainWindowTitle
在winfrom中调用的该值为空,在操作系统中直接执行的一定有值.