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();
}
}
it's gonna kill all the process with 'cmd' name but the ones that are parent for other processes.