如何在不杀死父进程[相同进程名称]的情况下终止子进程。 [英] How to kill child process without killing parent process [same process name].
本文介绍了如何在不杀死父进程[相同进程名称]的情况下终止子进程。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何仅杀死当前子进程而不影响C#中同名父进程。
我的父进程和子进程具有相同名称'setup'。有任何想法请分享。
谢谢。
How to kill only current child process without affecting to parent process of same name in C#.
my parent process and child process have same name 'setup'. Have any idea please share.
Thanks.
推荐答案
这里是代码
here is the code
private void button1_Click(object sender, EventArgs e)
{
Dictionary<int, int> listDictionary = new Dictionary<int, int>();
Process[] aProc = Process.GetProcessesByName("cmd");
foreach (Process proc in aProc)
{
ManagementObject managementObject = new ManagementObject("win32_process.handle='" + proc.Id + "'");
managementObject.Get();
int parentId = Convert.ToInt32(managementObject["ParentProcessId"]);
listDictionary.Add(proc.Id, parentId);
}
foreach (KeyValuePair<int, int> keyValuePair in listDictionary)
{
if (!listDictionary.ContainsValue(keyValuePair.Key))
Process.GetProcessById(keyValuePair.Key).Kill();
}
}
它会用'cmd杀死所有进程'name,但是那些是其他进程的父级。
it's gonna kill all the process with 'cmd' name but the ones that are parent for other processes.
这篇关于如何在不杀死父进程[相同进程名称]的情况下终止子进程。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文