views:

398

answers:

6

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?

A: 

It's possible, sure. Try one of these two:

  1. Start the process, then go to Debug->Attach and find the process. You may have to refresh to see it.
  2. Add a "Debugger.Break()" statement in the code, if possible; that will break automatically (but be sure to remove it or surround it with preprocessor directives so it doesn't get into production code).
roufamatic
A: 

In the projects settings "Debug" section there's a textbox for "Command line arguments:". When the VS debugger starts the C# program, it'll pass those arguments to the process just as if the program had been started form the command line with those arguments.

The alternative is to use a command line debugger. There are a few options here, but in all honesty they're probably not what you want to use instead of VS unless you're getting into some really hairy debugging scenarios. If you're interested in checking them out, there's a good summary in this SO answer:

You can also try the techique of putting a call to System.Diagnostics.Debugger.Break() early in your initialization - if the program is running under a debugger, it'll break, it it's not running under a debugger you should be asked if you want to attach one. You can make the call conditionally depending on a configuration file or environment variable setting so you only get the break if you're really interested in it (somewhat intrusive, but not too bad).

Michael Burr
A: 

Attaching to Cmd.exe should work - are you building in debug mode and running from the /bin/debug folder?

Lester
I'm running from bin\Debug, and yes, i'm building in debug mode
Chris
+1  A: 

As others have said, you can specify the stratup command line arguments from within the project and just start debugging within Visual Studio.

If you still want to attach to the running application, you need to attach the debugger to MyApp.exe (whatever your application is called - the exe that gets compiled to the bin\debug directory) and not cmd.exe. Attaching to cmd.exe it attaching to the command process, not the process of your application.

adrianbanks
A: 

To debug from the command line rather than using the VS GUI maze:

  • Launch the Visual Studio Command Prompt

  • type vsjitdebugger/? which gives you the command example like :

c:> vsjitdebugger [AppName] [Args] : Launch the specified executable and attach to debugger

  • typing tlist or tasklist will give you PIDs for attaching to existing processes. example:

c:> tasklist | find /i "web"

Kwame
A: 

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).
VladV