Robert
Robert

Reputation: 6437

Find the pid of Visual Studio which is debugging my process

I know I can find if a process is being debugged via a call to Debugger.IsAttached in .NET, but I'd like to be able to get the PID of the Visual Studio that is the debugging the processes. Is this possible?

Upvotes: 6

Views: 4261

Answers (2)

Gabe Halsmer
Gabe Halsmer

Reputation: 868

This worked for me.

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;
}

Upvotes: 0

Omer Raviv
Omer Raviv

Reputation: 11827

You could use the TryGetVSInstance method outlined in this answer to get a hold of each instance of Visual Studio's EnvDTE COM Automation object. Once you have that, just iterate the DTE.Debugger.DebuggedProcesses collection and check if any of them are pointing to the same processID as the process you're interested in.

Upvotes: 2

Related Questions