/// <summary>
/// 结束进程和相关的子进程
/// </summary>
/// <param name="pid">需要结束的进程ID</param>
public static void KillProcessAndChildren(int pid)
{
ManagementObjectSearcher searcher = new ManagementObjectSearcher("Select * From Win32_Process Where ParentProcessID=" + pid);
ManagementObjectCollection moc = searcher.Get();
try
{
Process proc = Process.GetProcessById(pid);
Console.WriteLine(pid);
proc.Kill();
}
foreach (ManagementObject mo in moc) { KillProcessAndChildren(Convert.ToInt32(mo["ProcessID"])); } catch (ArgumentException) { /* process already exited */ } }