/// <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();<br></br>
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 */ } }

标签: 进程, C#, proc, pid, Process, 递归, ManagementObjectSearcher, KillProcessAndChildren

相关文章推荐

添加新评论,含*的栏目为必填