P
Peter Steele
It's easy enough to enumerate the current processes in a system, but how can
I find the parent process for a given process?
I find the parent process for a given process?
Peter Steele said:It's easy enough to enumerate the current processes in a system, but how
can I find the parent process for a given process?
Willy Denoyette said:Easy enough when using System.Management...
Here's a sample:
public static void Main() {
Process p = Process.GetCurrentProcess();
int parentPid = GetParentProcess(p.Id);
Console.WriteLine(parentPid);
}
static int GetParentProcess(int Id)
{
int parentPid=0;
using(ManagementObject mo = new ManagementObject("win32_process.handle='"
+ Id.ToString() + "'"))
{
mo.Get();
parentPid = Convert.ToInt32(mo["ParentProcessId"]);
DumpProcessProperties(parentPid);
}
return parentPid;
}
// dump proces properties to the console
static void DumpProcessProperties(int Id)
{
using(ManagementObject mo = new ManagementObject("win32_process.handle='"
+ Id.ToString() + "'"))
{
mo.Get();
foreach(PropertyData pd in mo.Properties)
{
Console.WriteLine("Property: {0}, Value: [{1}]",pd.Name, pd.Value);
}
}
}
Willy.
Peter Steele said:It's easy enough to enumerate the current processes in a system, but how
can I find the parent process for a given process?