How to get parent process in .NET in managed way

后端 未结 6 596
伪装坚强ぢ
伪装坚强ぢ 2020-11-22 03:39

I was looking a lot for method to get parent process in .NET, but found only P/Invoke way.

6条回答
  •  青春惊慌失措
    2020-11-22 04:20

    Here's my try at a managed solution.

    It polls the performance counters for all processes and returns a dictionary of child PID to parent PID. Then you can check the dictionary with your current PID to see your parent, grandparent, etc.

    It is overkill in how much info it gets, for sure. Feel free to optimize.

    using System;
    using System.Collections.Generic;
    using System.Diagnostics;
    
    namespace PidExamples
    {
        class ParentPid
        {
            static void Main(string[] args)
            {
                var childPidToParentPid = GetAllProcessParentPids();
                int currentProcessId = Process.GetCurrentProcess().Id;
    
                Console.WriteLine("Current Process ID: " + currentProcessId);
                Console.WriteLine("Parent Process ID: " + childPidToParentPid[currentProcessId]);
            }
    
            public static Dictionary GetAllProcessParentPids()
            {
                var childPidToParentPid = new Dictionary();
    
                var processCounters = new SortedDictionary();
                var category = new PerformanceCounterCategory("Process");
    
                // As the base system always has more than one process running, 
                // don't special case a single instance return.
                var instanceNames = category.GetInstanceNames();
                foreach(string t in instanceNames)
                {
                    try
                    {
                        processCounters[t] = category.GetCounters(t);
                    }
                    catch (InvalidOperationException)
                    {
                        // Transient processes may no longer exist between 
                        // GetInstanceNames and when the counters are queried.
                    }
                }
    
                foreach (var kvp in processCounters)
                {
                    int childPid = -1;
                    int parentPid = -1;
    
                    foreach (var counter in kvp.Value)
                    {
                        if ("ID Process".CompareTo(counter.CounterName) == 0)
                        {
                            childPid = (int)(counter.NextValue());
                        }
                        else if ("Creating Process ID".CompareTo(counter.CounterName) == 0)
                        {
                            parentPid = (int)(counter.NextValue());
                        }
                    }
    
                    if (childPid != -1 && parentPid != -1)
                    {
                        childPidToParentPid[childPid] = parentPid;
                    }
                }
    
                return childPidToParentPid;
            }
        }
    }    
    

    In other news, I learned how many performance counters there were on my machine: 13401. Holy cow.

提交回复
热议问题