.net 如何将进程附加到 Visual Studio 中的调试器?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/986470/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 12:51:16  来源:igfitidea点击:

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

.netvisual-studiodebugging

提问by Bertvan

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

我知道我可以使用Process.Start(). 是否也可以将调试器附加到该进程?

Not from code per se, but just a way to do it?

不是来自代码本身,而是一种方法?

回答by George Stocker

You can attach to a running processusing Tools | Attach to Process. If it's a Web Application, you can attach to it by attaching to aspnet_wp.exeor w3wp.exe.

您可以附加到正在运行的进程使用Tools | Attach to Process。如果它是一个 Web 应用程序,您可以通过附加到aspnet_wp.exe或来附加到它w3wp.exe

To answer your question on how to attach to a process programmatically:

要回答有关如何以编程方式附加到流程的问题:

Here are other Stack Overflow questions that deal with that:

以下是处理该问题的其他 Stack Overflow 问题:

回答by dove

In visual studio click Tools | Attach to process. Then select appropriate service.

在visual studio中点击工具| 附加到进程。然后选择合适的服务。

回答by samoz

You can do this in pretty much any debugger worth its salt.

您几乎可以在任何值得一试的调试器中执行此操作。

Visual Studio has one that should fit your needs.

Visual Studio 有一款应该可以满足您的需求。

If you need a little more advanced control, try OllyDbg, which is a disassembler, so you can actually manipulate your program at the assembly level. This will give you complete control, but it might be information overload as well.

如果您需要更高级的控制,请尝试OllyDbg,它是一个反汇编程序,因此您可以在汇编级别实际操作您的程序。这将为您提供完全的控制权,但也可能是信息过载。

回答by Samrat Debroy

In Visual Studio 2015, click 'Debug > Attach to process' in the menu. Alternatively, there is a shortcut key Ctrl+Alt+P.

在 Visual Studio 2015 中,单击菜单中的“调试 > 附加到进程”。或者,有一个快捷键 Ctrl+Alt+P。

回答by Alpesh

You can do this in your code.

您可以在代码中执行此操作。

public static void Attach(DTE2 dte)
        {
            var processes = dte.Debugger.LocalProcesses;
            foreach (var proc in processes.Cast<EnvDTE.Process>().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());