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?
假设我希望通过System.Diagnostics.Process类的属性检查当前正在执行的进程。是否可以使用该进程加载此类的实例(即,以某种方式将Process对象附加到进程),还是必须使用Start方法启动它?
2 个解决方案
#1
If you know the PID :
如果你知道PID:
Process p = Process.GetProcessById(id);
If you know the name :
如果你知道这个名字:
Process p = Process.GetProcessesByName(name).FirstOrDefault();
#2
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.
您无法附加到它,但您可以使用Process.GetProcesses方法枚举计算机上的所有正在运行的进程。其中一个将是您正在寻找的过程。
var list = System.Diagnostics.Process.GetProcesses();
foreach ( var proc in list ) {
// Determine if it's the process and use it
}
#1
If you know the PID :
如果你知道PID:
Process p = Process.GetProcessById(id);
If you know the name :
如果你知道这个名字:
Process p = Process.GetProcessesByName(name).FirstOrDefault();
#2
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.
您无法附加到它,但您可以使用Process.GetProcesses方法枚举计算机上的所有正在运行的进程。其中一个将是您正在寻找的过程。
var list = System.Diagnostics.Process.GetProcesses();
foreach ( var proc in list ) {
// Determine if it's the process and use it
}