5

I am running an inherited project written in C# inside Visual Studio Code. In order for this application to run, it needs to take command line input (-t, -h, etc). How do I test this from inside Visual Studio?

Currently (I've been learning dotnet, C#, VS, etc as I go) I have a hello world program I can run from vsc's terminal. For a reason I haven't been able to pinpoint, probably how I installed it, dotnet run isn't recognized - I have to feed it an explicit path to dotnet.exe: C:\Program Files\dotnet\dotnet.exe run

How can I do this when the program requires command line input? My shot in the dark of C:\Program Files\dotnet\dotnet.exe run -t predictably didn't work, but I'm not sure what else to try.

Thanks!

4
  • Wait, so what you are saying is that, when you run/debug(F5) the project, you have to go to cmd and type in the path then run before the application can run, is that right? Commented May 25, 2017 at 23:13
  • Check the section "To build and run the Command Line Parameters samples within Visual Studio" in msdn.microsoft.com/en-us/library/cs8hbt1w(v=vs.90).aspx Commented May 25, 2017 at 23:13
  • @Malky.Kid, right. F5 isn't quite working - I suspect I just have the project set up wrong - so I'm bypassing it entirely and just using the command line until I can work out this first problem Commented May 25, 2017 at 23:31
  • @EmilyColeman why not use an express edition of visual studio? It's free Commented May 26, 2017 at 6:47

5 Answers 5

5

Run with Terminal

When you run your code using terminal you must add ' -- ' to tell dotnet you are running your code with an argument/s

C:>dotnet run -- <your arguments here>

Run with Debugger

  • Locate your .vscode folder inside your project if not create one.
  • Open the launch.json file
  • You will see a json object and add your arguments inside the "args" key.

launch.json

    "configurations": [
        {
            "name": ".NET Core Launch (console)",
            "args": [], // PUT YOUR ARGUMENTS HERE
             ...
        }
    ]
Sign up to request clarification or add additional context in comments.

Comments

4

If you are using dotnet.exe run to start your application you need add the -- switch statement to instruct dotnet.exe to pass the arguments to your application. For example

Microsoft Documentation

dotnet.exe run -- -arg1 -arg2 (etc) notice the -- after the dotnet arguments and before your program specific arguments.

GitHub Issue

Comments

1

Since OP is specifically asking about how to do this in Visual Studio Code, they are likely using VSCode's run feature (not the dotnet CLI as other answers assume), to run their application. In this case, the proper way to supply command line arguments is via the .vscode\launch.json file.

Add an args array attribute to your configuration and populate it with the arguments you would like to pass.

"configurations": [
    {
        // ... ,
        "args": ["-arg1", "-arg2"]
    }

Comments

0

I tried to add a comment to Nico's answer but I lack sufficient reputation points. I was confused by the dash character in front of each arg: "-arg1 -arg2 (etc)". For clarity I would like to point out that .NET Core 2.1 seems to not need this. In the case of my console app, it takes a date for the first arg, an integer for the second, then an operator (+ or -) for the third arg. If I entered the following:

C:\>dotnet run -- -7/13/2018 -30 -+

I found that the leading dash in front of each arg was passed into the program along with the intended arg and I ended up trying to date parse "-7/13/2018"

I got the expected result when I entered it like this:

C:\>dotnet run -- 7/13/2018 30 +

1 Comment

Dashes are just convention for programs that accept command line arguments, usually representing flags for the program. they are passed exactly as-is, and if the programmer decides that they want users to type the dashes (again, convention), then they will have to strip them from the input themselves within the program
-1
  1. Right click on your project
  2. Click Properties
  3. Click Debug in the Properties window
  4. Under "start options:"
  5. Add a "Command Line Argument:" = run -t
  6. Add a "Working Directory:" try the bin\debug directory

1 Comment

I expect the flag is because this answer applies to Visual Studio not VSCode.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.