1

I need to keep track of the Ids of the processes spawned from my program, including the id of my C# application's root process i.e. the process the user started by double-clinking its icon.

Has anyone else needed to do this?

What's a good reliable way of doing this?

I'm imagining a List(), but a DataTable might be even better depending on available data.

WonderWorker
  • 8,539
  • 4
  • 63
  • 74
  • What is it you call the "root process"? Does you app spawn additional processes? – Thomas Levesque Mar 03 '16 at 14:46
  • @Thomas Levesque I didn't know how else to describe it. By root process I mean the first process created when users of my app double click the exe – WonderWorker Mar 03 '16 at 14:50
  • 2
    Usually, an app will only be a single process. Are you perhaps mixing terminology up here? – Damien_The_Unbeliever Mar 03 '16 at 14:52
  • @BugFinder I tried Process.GetProcessId(), but I read that it does not guarantee the process id of the process calling it. See: http://stackoverflow.com/questions/29354906/net-get-process-id-of-my-application-in-c-sharp – WonderWorker Mar 03 '16 at 14:53
  • @Damien_The_Unbeliever I possibly am. Are threads different to processes? – WonderWorker Mar 03 '16 at 14:54
  • You surely mixed it up: If you read the answer to the SO question you just mentioned, you'll see that `Process.GetCurrentProcess().Id` is the correct way to go. – René Vogt Mar 03 '16 at 15:00
  • @René Vogt I see, I was lumping threads and processes into the same pot. So, that solves part of my problem. All that's left is how to find the id if my application launches a process? Does that process record its parent's process id, or do I have to record them manually using something similar to zhaojingbo's method below? – WonderWorker Mar 03 '16 at 16:47
  • Parent process IDs seem somewhat tricky on windows. Please refer to http://stackoverflow.com/questions/7486717/finding-parent-process-id-on-windows and use the `ManagementObjectSearcher` as in zhaojingbo's answer. – René Vogt Mar 03 '16 at 17:03
  • I think I know how to solve this. I'll code it up and post the code. – WonderWorker Mar 04 '16 at 14:06

2 Answers2

1
 string process = "notepad.exe";
 string sql = string.Format("select * from win32_process where name='{0}'", process);
 ManagementObjectSearcher searcher = new ManagementObjectSearcher(sql);
 StringBuilder sb = new StringBuilder();
 foreach (ManagementObject mo in searcher.Get())
 {
       string pid = mo.Properties["ProcessId"].Value.ToString();
       string cimtime = mo.Properties["CreationDate"].Value.ToString();

       cimtime = cimtime.Substring(0, cimtime.IndexOf('.'));
       string format = "yyyyMMddHHmmss";
       DateTime startTime = DateTime.ParseExact(cimtime, format, System.Globalization.CultureInfo.CurrentCulture);
       sb.AppendFormat("PID:{0}\t启动时间:{1}", pid, startTime);
       sb.AppendLine();
 }
 MessageBox.Show(sb.ToString());

enter image description here

zhaojingbo
  • 131
  • 6
0
Process currentProcess = Process.GetCurrentProcess();
var id = currentProcess.Id;

You will need a refernce to

using System.Diagnostics;
James Dev
  • 2,979
  • 1
  • 11
  • 16