using System; using System.Collections.Generic; using System.Diagnostics; using System.Linq; using System.Text; //---------------------------------------------------------------- //Module Name: $safeprojectname$ //Purpose: //CopyRight: Muchinfo //History: //---------------------------------------------------------------- //DateTime 2016/8/22 14:26:52 //Author //Description Create //---------------------------------------------------------------- namespace Muchinfo.MTPClient.Infrastructure.Extensions { public static class ProcessExtensions { private static string FindIndexedProcessName(int pid) { var processName = Process.GetProcessById(pid).ProcessName; var processByName = Process.GetProcessesByName(processName); string processIndexdName = null; for (int index = 0; index < processByName.Length; index++) { processIndexdName = index == 0 ? processName : processName + "#" + index; var procssId = new PerformanceCounter("Process", "ID Process", processIndexdName); if ((int) procssId.NextValue() == pid) { return processIndexdName; } } return processIndexdName; } private static Process FindPidFromIndexedProcessName(string indexedProcessName) { var parentId = new PerformanceCounter("Process", "Creating Process ID", indexedProcessName); return Process.GetProcessById((int) parentId.NextValue()); } public static Process Parent(this Process process) { return FindPidFromIndexedProcessName(FindIndexedProcessName(process.Id)); } } }