[英]Find the pid of Visual Studio which is debugging my process
我知道我可以通过调用.NET中的Debugger.IsAttached来查找是否正在调试进程,但是我希望能够获得调试进程的Visual Studio的PID。 这可能吗?
您可以使用本答案中概述的TryGetVSInstance
方法来获取Visual Studio的EnvDTE COM Automation对象的每个实例。 完成后,只需迭代DTE.Debugger.DebuggedProcesses集合并检查它们中是否有任何一个指向与您感兴趣的进程相同的processID。
这对我有用。
public static Process GetParent(Process process)
{
var processName = process.ProcessName;
var nbrOfProcessWithThisName = Process.GetProcessesByName(processName).Length;
for (var index = 0; index < nbrOfProcessWithThisName; index++)
{
var processIndexdName = index == 0 ? processName : processName + "#" + index;
var processId = new PerformanceCounter("Process", "ID Process", processIndexdName);
if ((int)processId.NextValue() == process.Id)
{
var parentId = new PerformanceCounter("Process", "Creating Process ID", processIndexdName);
return Process.GetProcessById((int)parentId.NextValue());
}
}
return null;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.