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

cconsole-applicationdebugging

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?

Best Answer

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).
Related Topic