tags:

views:

227

answers:

2

Suppose I wish to examine a currently executing process via the properties of the System.Diagnostics.Process class. Is it possible to load an instance of this class with that process (i.e., somehow attach a Process object to the process), or does one have to have started it with the Start method?

+2  A: 

You can't attach to it but you can use the Process.GetProcesses method to enumerate all of the running process on the machine. One of those will be the process you are looking for.

var list = System.Diagnostics.Process.GetProcesses();
foreach ( var proc in list ) {
  // Determine if it's the process and use it
}
JaredPar
+2  A: 

If you know the PID :

Process p = Process.GetProcessById(id);

If you know the name :

Process p = Process.GetProcessesByName(name).FirstOrDefault();
Thomas Levesque