问题描述
我开始Internet Explorer的编程与code,它是这样的:
I am starting Internet Explorer programatically with code that looks like this:
ProcessStartInfo startInfo = new ProcessStartInfo("iexplore.exe");
startInfo.WindowStyle = ProcessWindowStyle.Hidden;
startInfo.Arguments = "http://www.google.com";
Process ieProcess = Process.Start(startInfo);
这会产生2个进程在Windows任务管理器中可见。于是,我试图杀掉进程:
This generates 2 processes visible in the Windows Task Manager. Then, I attempt to kill the process with:
ieProcess.Kill();
这导致被关闭在任务管理器的进程,并在其他遗迹之一。我试图检查,将有子进程的任何属性,但没有发现。我如何能杀死其他进程也?更一般地,你怎么杀你开始的Process.Start与进程相关联的所有进程?
This results in one of the processes in Task Manager being shut down, and the other remains. I tried checking for any properties that would have children processes, but found none. How can I kill the other process also? More generally, how do you kill all the processes associated with a process that you start with Process.Start?
推荐答案
这工作很漂亮对我来说:
This worked very nicely for me:
/// <summary>
/// Kill a process, and all of its children, grandchildren, etc.
/// </summary>
/// <param name="pid">Process ID.</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 (ArgumentException)
{
// Process already exited.
}
}
这篇关于在C#编程杀死进程树的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!