/// <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 */ } }