C#下查并杀死⼦进程(进程树)
参考:
/**
* 传⼊参数:⽗进程id
* 功能:根据⽗进程id,杀死与之相关的进程树
*/
public static void KillProcessAndChildren(int pid)
{
ManagementObjectSearcher searcher = new ManagementObjectSearcher("Select * From Win32_Process Where ParentProcessID=" + pid);    ManagementObjectCollection moc = searcher.Get();
foreach (ManagementObject mo in moc)
{
KillProcessAndChildren(Convert.ToInt32(mo["ProcessID"]));
}
try
{
Process proc = Process.GetProcessById(pid);
Console.WriteLine(pid);
proc.Kill();
}
catch (ArgumentException)
{
/
* process already exited */
}
}
  常⽤的Process⽅法:
//1.根据进程id,获得进程
Process p = Process.GetProcessById(100);
//2.获取当前进程
Process p = Process.GetCurrentProcess();
//3.根据进程名字获取进程,返回的结果是⼀个数组
Process p = (Process.GetProcessesByName("DriverEasy"))[0];
特别声明:因为代码中使⽤到了 ManagementObjectSearcher,如果在Visual Studio中即使引⼊了命名空间,还是提⽰⽆法到相应的类的话,那么按下⾯说的来做,
第⼀步:在项⽬上⿏标右键 -> add -> Reference
第⼆步:到 System.Management,把它前⾯的勾打上
exited
OK了

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。