32 lines
840 B
C#
32 lines
840 B
C#
using System.Diagnostics;
|
|
using System.Linq;
|
|
using System.Management;
|
|
|
|
namespace AlmToolkit
|
|
{
|
|
public static class ProcessExtensions
|
|
{
|
|
public static Process GetParent(this Process process)
|
|
{
|
|
try
|
|
{
|
|
using (var query = new ManagementObjectSearcher(
|
|
"SELECT ParentProcessId " +
|
|
"FROM Win32_Process " +
|
|
"WHERE ProcessId=" + process.Id))
|
|
{
|
|
return query
|
|
.Get()
|
|
.OfType<ManagementObject>()
|
|
.Select(p => Process.GetProcessById((int)(uint)p["ParentProcessId"]))
|
|
.FirstOrDefault();
|
|
}
|
|
}
|
|
catch
|
|
{
|
|
return null;
|
|
}
|
|
}
|
|
}
|
|
}
|