raven
raven

Reputation: 18145

Is it possible to attach a .NET System.Diagnostics.Process object to a running process?

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?

Upvotes: 2

Views: 825

Answers (2)

Thomas Levesque
Thomas Levesque

Reputation: 292695

If you know the PID :

Process p = Process.GetProcessById(id);

If you know the name :

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

Upvotes: 4

JaredPar
JaredPar

Reputation: 755457

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
}

Upvotes: 2

Related Questions