How do I attach a process to the debugger in Visual Studio?

前端 未结 5 1227
既然无缘
既然无缘 2020-12-08 11:36

I know I can start a process in code with Process.Start(). Is it also possible to attach the debugger to that process?

Not from code per se ,

5条回答
  •  佛祖请我去吃肉
    2020-12-08 12:06

    You can do this in your code.

    public static void Attach(DTE2 dte)
            {
                var processes = dte.Debugger.LocalProcesses;
                foreach (var proc in processes.Cast().Where(proc => proc.Name.IndexOf("YourProcess.exe") != -1))
                    proc.Attach();
            }
    
            internal static DTE2 GetCurrent()
            {
                var dte2 = (DTE2)Marshal.GetActiveObject("VisualStudio.DTE.12.0"); // For VisualStudio 2013
    
                return dte2;
            }
    

    Usage:

    Attach(GetCurrent());
    

提交回复
热议问题