3

I've created a .NET 4.5 console application (an .exe file) with c# that processes some data in various files. The whole thing runs in less than one second. I have a problem with a testing version of the .exe that I'd like to step through in the Visual Studio 2013 debugger using particular input files (the filenames are passed as command line arguments). I can't do an "Attach to process..." since the process has completed too quickly to attach to it. How do I debug in this case.

Note that I'm from the python world, and the python equivalent of what I'm trying to do is python -m pdb pdb_script.py.

Thanks for any advice!

3
  • 3
    Have you tried adding breakpoints to your code? Click the margin to the left of the code you want it to pause on to add a breakpoint. Commented May 11, 2015 at 15:48
  • Why do you need to attach to the running process? Just run it from the debugger natively, assuming you still have the code files and project, solution files, etc. Commented May 11, 2015 at 15:49
  • Yes I can add breakpoints just fine. The issue I have is that I want to run these steps: 1. Set up debugging in some way for the .exe (this is the part I don't know how to do; 2. from the cmd window do a myprog.exe file1 file2; 3. Have the debugger pop open and step through the code. I know I'm missing something here, but I don't know what it is! Commented May 11, 2015 at 15:49

3 Answers 3

5

Right click on your project and go to "properties".

Then select the "debugging" tab on the left-hand side.

There's a box where you can input the desired command line arguments for use when running in debug mode.

EDIT: If you're asking how to start the debugger, then add some breakpoints to your code, then right click on the project in your solution and pick Debug > Start New Instance.

Alternatively you can right click and choose "Set as Startup Project", after which you can start debugging with F5 or the "Start" button at the top of the UI.

Sign up to request clarification or add additional context in comments.

2 Comments

As far as I see the question the OP is asking how to attach to the process not how to add arguments to it.
@IgnacioSolerGarcia On re-reading the question, you could be right. It's not entirely clear where the problem is. I will edit with more advice.
4

Add the following line in your code:

System.Diagnostics.Debugger.Break(); 

This will allow you to debug your application before it ends.

1 Comment

While the other answers are not wrong, this is the use case that I had in mind that matches the python python -m pdb pdb_script.py idiom. Thank you!
1

In Visual Studio, you can add commandline arguments before starting a debugging session by right-clicking your project, selecting properties and then going down to the debug tab.

Then you can start it in Visual Studio with f5

If that's too much trouble (because you are going to change the arguments a lot), you could do something like adding a Console.ReadLine to your program at the beginning that will give you a change to attach a debugger. You could even have an extra command line argument for debugging that will only pause for you to attach the debugger if you pass that argument.

2 Comments

As far as I see the question the OP is asking how to attach to the process not how to add arguments to it.
@IgnacioSolerGarcia: They are asking how to debug a console app that they need to pass arguments to. If you pass the arguments in the properties then you can debug it just like any other process with f5

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.