1、获取(删除)所有进程与子进程
using System.Management;
/// <summary>
/// 删除进程及其所有子进程
/// </summary>
/// <param name="pid"></param>
private 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);
proc.Kill();
}
catch (Exception ex)
{
Soft.Logging.SoftLogger.Logger.Error(ex);
}
}