Debugging: Attach to Process for Console App running inside cmd.exe

Chris picture Chris · May 3, 2010 · Viewed 38.6k times · Source

How do you "Attach to Process..." for a console application thats running from a CMD window and not launched by F5? The reason I ask is because the application takes command line arguments and I want to have a genuine experience.

I've even attaching to CMD.exe, but no luck, or setting a break-point using Console.ReadKey() with also no luck. I'm kind of at a loss here.

Is this possible?

Answer

VladV picture VladV · May 18, 2010

You have some options:

  • Use "Debug -> Command line arguments" option in Visual Studio;
  • Use "Debug -> Attach to process" and find your process; it is not cmd.exe, but a process with executable name like "MyProject.exe". You can use Process Explorer or another task manager with "tree view" support to easily find the Process ID - just look for the processes started by your cmd.exe.
  • Put Debugger.Break() into your code - when this method is executed, the system will launch a dialog asking you to choose what instance of Visual Studio to use for debugging (you can choose the one with your project already open).