Okay I have found the answer myself. I am sharing it just in case someone needs it.
int myappid = Process.GetCurrentProcess().Id;
Process[] processes = Process.GetProcessesByName("EXCEL");
foreach (Process prs in processes)
{
var query = string.Format("SELECT ParentProcessId FROM Win32_Process WHERE ProcessId = {0}", prs.Id);
var search = new ManagementObjectSearcher("root\\CIMV2", query);
var results = search.Get().GetEnumerator();
results.MoveNext();
var queryObj = results.Current;
var parentId = (uint)queryObj["ParentProcessId"];
if (ProcessExists((int)parentId))
{
var parent = Process.GetProcessById((int)parentId);
if (parent.Id == myappid)
{
prs.Kill();
}
}
}
private bool ProcessExists(int id)
{
return Process.GetProcesses().Any(x => x.Id == id);
}
In the above code Process[] processes = Process.GetProcesses();
would give me all the processes. I am looping though the collection and finding parent process id of each. If parent id matches my application process id and the name of the child process is excel, I kill it.
solved Get a list of all excel processes started by winform application