How to debug a C# command-line program

后端 未结 6 1658
说谎
说谎 2021-02-07 04:11

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,

相关标签:
6条回答
  • 2021-02-07 04:22

    Either add a breakpoint to the opening { of Main, or step into the program (Debug menu). At that point set a watch on the parameter for main (the command line arguments) by selecting it and either right-click/Add Watch or drag the parameter to to watch window if it is already open. Double-click the Value column in the Watch window and set it to whatever you want it to be.

    Note: the value added must be valid code -- that is, to add a -help to the string[] you would have to type new string[] {"-help"} or new [] {"-help"} depending on the version you are using.

    This has the advantage over setting the parameter in the Debug tab of the Properties window by allowing different parameters for each run without having to return to the Properties window.

    0 讨论(0)
  • 2021-02-07 04:30

    You can use Visual Studio to attach a debugger to the command line application, once it is under way with the correct arguments. I'm not sure if your application will terminate quickly or give you any opportunity to attach the debugger, but if it will, this should work.

    I'm using VS2008 but I'll bet the process is similar in 2010:

    1. In VS, go to Tools and click Attach to Process
    2. Choose your application from the list and press Attach

    Now VS should be able to finagle its way into your application and break on an error.

    0 讨论(0)
  • 2021-02-07 04:33

    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.

    0 讨论(0)
  • 2021-02-07 04:35

    When you have command line arguments then you need to follow different route to debug. Go to project and then select properties. There you will see debug section and then give required parameters. So that it will take care of passing parameters to the program. If you have multiple parameters then separate them with space. After that if you debug it will hit the break point directly. For more info check here how to debug c# through command line arguments in visual studio

    0 讨论(0)
  • 2021-02-07 04:36

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

    0 讨论(0)
  • 2021-02-07 04:43

    You just need to add a breakpoint to the first line of the main function (you can do this by clicking on the line in the Visual Studio editor and hitting F9) and hit F5 to start a debug session.

    0 讨论(0)
提交回复
热议问题