Click here to Skip to main content
15,662,532 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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.

[no name] 22-Jun-14 21:06pm    
The Process class an ID property that you could use to identify your process. More information might help you get a better answer.
Jafarinejadvazifehkhorani 22-Jun-14 23:29pm    
if you provide the code, it is gonna be very helpful
SachinSutar 23-Jun-14 5:49am    
I just tried this and it close parent also.

Process[] aProc = Process.GetProcessesByName(processname);
forech((Process proc in aProc)

I know this will kill all the process having same name. Want to get parent process from this collection. Or if any other soloution please share.

Sergey Alexandrovich Kryukov 23-Jun-14 0:19am    
I would also ask: why killing any process? Is it your process? A need for killing some process is always a sign of someone's bug or other defect, including wrong design.
SachinSutar 23-Jun-14 5:45am    
Yes. It is my own process. I run multiple process [same parent process with different arguments] inside one parent process on button click & i want to close only child processes without affecting parent.

1 solution

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 + "'");
             int parentId = Convert.ToInt32(managementObject["ParentProcessId"]);
             listDictionary.Add(proc.Id, parentId);
         foreach (KeyValuePair<int, int> keyValuePair in listDictionary)
             if (!listDictionary.ContainsValue(keyValuePair.Key))

it's gonna kill all the process with 'cmd' name but the ones that are parent for other processes.
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900