无法调试使用Process.Start()启动的项目

我在同一个解决方案中有两个C#WinForm项目,我们称它们为A和B.项目A通过如下调用启动进程B.

ProcessStartInfo psi = new ProcessStartInfo(); psi.FileName = Task.EXEFilename; psi.WorkingDirectory = Path.GetDirectoryName(Data.EXEFilename); Process.Start(psi); 

进程B正确启动。 我想在调试A时调试进程B.我想在B中设置一个断点就足够了,但它永远不会被击中。 我已经validation了启动的进程是在B的bin / debug文件夹中。在这种情况下我不应该附加到进程来将调试从A切换到B?

在第二个项目中检查它的命令行参数,如果它看到像--debug作为第一个参数传入的第二个程序启动调试器本身

 private static void Main(string[] args) { //If no debugger is attached and the argument --debug was passed launch the debugger if (args.Length == 1 && args[0] == "--debug" && Debugger.IsAttached == false) Debugger.Launch(); //(snip) the rest of your program } 

执行此操作时,您将看到一个对话框窗口,允许您选择打开Visual Studio的新副本或仅使用已打开的副本。

图片


您还可以将子进程放在Image File Execution Options注册表项中 。

听起来您希望Visual Studio自动附加到调试期间创建的任何子进程。 像windbg这样的其他调试器有这种行为,但不幸的是Visual Studio没有。 有一个用户语音项目正在跟踪此请求

短期虽然最好的选择是简单地在子进程生成时断开并手动附加调试器。

 var proc = Process.Start(psi); Debugger.Break(); 

尝试从程序B获取进程ID,例如:

 ProcessStartInfo psi = new ProcessStartInfo(); psi.FileName = Task.EXEFilename; psi.WorkingDirectory = Path.GetDirectoryName(Data.EXEFilename); var proc = Process.Start(psi); Debug.WriteLine(proc.Id); 

然后在另一个Visual Studio实例中加载项目,并使用Debug> Attach to process附加到Program B.