Monitor child processes of a process

前端 未结 3 2082
遥遥无期
遥遥无期 2020-12-11 00:35

I\'m running .exe file using this code:

Process proc = Process.Start(\"c:\\program.exe\");
proc.WaitForExit();

If I start Stopwatch

3条回答
  •  情话喂你
    2020-12-11 01:11

    Here is the solution that the asker found:

    public static class ProcessExtensions
    {
        public static IEnumerable GetChildProcesses(this Process process)
        {
            List children = new List();
            ManagementObjectSearcher mos = new ManagementObjectSearcher(String.Format("Select * From Win32_Process Where ParentProcessID={0}", process.Id));
    
            foreach (ManagementObject mo in mos.Get())
            {
                children.Add(Process.GetProcessById(Convert.ToInt32(mo["ProcessID"])));
            }
    
            return children;
        }
    }
    

提交回复
热议问题