1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859 |
- using System;
- using System.Diagnostics;
- using System.Windows.Forms;
- namespace TestProcess
- {
- public static class ProcessExtensions
- {
- private static string FindIndexedProcessName(int pid)
- {
- var processName = Process.GetProcessById(pid).ProcessName;
- var processesByName = Process.GetProcessesByName(processName);
- string processIndexdName = null;
- for (var index = 0; index < processesByName.Length; index++)
- {
- processIndexdName = index == 0 ? processName : processName + "#" + index;
- var processId = new PerformanceCounter("Process", "ID Process", processIndexdName);
- if ((int)processId.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));
- }
- }
- public partial class Form1 : Form
- {
- public Form1()
- {
- InitializeComponent();
- }
- private void Form1_Load(object sender, EventArgs e)
- {
- string sysdir = Environment.GetFolderPath(Environment.SpecialFolder.System);
- MessageBox.Show(sysdir);
- string pfile = Process.GetCurrentProcess().Parent().ProcessName;
- MessageBox.Show(pfile);
- }
- }
- }
|