Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to debug a C# command-line program

Tags:

I'm trying to build a command-line tool in C# with VS2010.

My question is: how do I debug this, like I would a winforms.
With winforms, I can step through the code, see the values at each individual step, etc...

Here however, the program only responds when I talk to it from the command line. I can't start a debug session, since that will first fully start the program. And due to the nature of the program, it will shut itself because there were no command-line arguments.

So no debugging can occur.

What do I need to do here?

EDIT: an example, since someone made a comment that makes me feel this explanation is needed.

C:\Path\To\File\file.exe -help

That is an example of how this program is adressed. The command, -help, is given in the same line that the program is started. In other words, I cannot first start the program, and then give it a command while it's running... because it won't be running anymore. It'll start, see that it had no arguments on startup, and because of that, shut down. That's how a command-line tool works.

  1. Activate
  2. Process possible arguments
  3. Output results
  4. Shut down automatically

It is not something that keeps running till you click the little x in the top right corner.

like image 915
KdgDev Avatar asked Sep 04 '10 22:09

KdgDev


People also ask

What is debugging techniques in C?

Definition: The important technique to find and remove the number of errors or bugs or defects in a program is called Debugging. It is a multistep process in software development. It involves identifying the bug, finding the source of the bug and correcting the problem to make the program error-free.

Can you debug C in Visual Studio?

Visual Studio Code supports the following debuggers for C/C++ depending on the operating system you are using: Linux: GDB. macOS: LLDB or GDB. Windows: the Visual Studio Windows Debugger or GDB (using Cygwin or MinGW)


2 Answers

In the Project properties, under Debug, you can enter any Command Line Arguments you would like, and then run the app with F5, the debugger will be attached automatically.

like image 101
davisoa Avatar answered Oct 04 '22 01:10

davisoa


You could add a call to Debugger.Launch to your startup code. Then you can compile, and start your app from the command line. You'll get a prompt asking you which debugger you want to attach (typically this will be a list of the different versions of Visual Studio you have installed), and away you go.

(But really, setting command-line parameters in Project properties > Debug tab is the better way to go most of the time. If that's not working for you, you should figure out why.)

like image 21
Joe White Avatar answered Oct 04 '22 00:10

Joe White