Feedback

C# - Get Instance Name From ProcessId

Veröffentlicht von am 10/26/2016
(0 Bewertungen)
Get Instance Name From ProcessId
public static string GetInstanceNameForProcessId(int processId)
    {
        var process = Process.GetProcessById(processId);
        string processName = Path.GetFileNameWithoutExtension(process.ProcessName);

        PerformanceCounterCategory cat = new PerformanceCounterCategory("Process");
        string[] instances = cat.GetInstanceNames()
            .Where(inst => inst.StartsWith(processName))
            .ToArray();

        foreach (string instance in instances)
        {
            using (PerformanceCounter cnt = new PerformanceCounter("Process",
                "ID Process", instance, true))
            {
                int val = (int)cnt.RawValue;
                if (val == processId)
                {
                    return instance;
                }
            }
        }
        return null;
    }
Abgelegt unter Get, Instance, Name, From, ProcessId, c#, csharp.

Kommentare zum Snippet

 

Logge dich ein, um hier zu kommentieren!