查找指定的进程并Kill的算法设计
public static bool FindAndKillProcessByName(string name)
{
//Parameter check
if (0 == name.Length)
{
return false;
}
//Find the named process and terminate it
foreach (Process winProc in Process.GetProcesses())
{
//use equals for the task in case we kill
//a wrong process
if (winProc.ProcessName.Equals(name))
{
winProc.Kill();
LoggingService.Info(winProc.ProcessName + " killed !");
return true;
}
}
return false;
}
private void btnOK_Click(object sender, EventArgs e)
{
this.FindProcessAndKill(this.textBox1.Text);
}
private bool FindProcessAndKill(string name)
{
string proName = name.Trim();
if (0==name.Length)
{
MessageBox.Show("输入的进程名无效,不能为空字符串!");
return false;
}
else
{
foreach (Process item in Process.GetProcesses())
{
if (item.ProcessName == proName)
{
item.Kill();
MessageBox.Show("进程已经找到,已经结束了该进程.");
return true;
}
}
}
MessageBox.Show("找不到指定进程!");
return false;
}