问题
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!
回答1:
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
回答2:
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
...
}
]
回答3:
- Right click on your project
- Click Properties
- Click Debug in the Properties window
- Under "start options:"
- Add a "Command Line Argument:" = run -t
- Add a "Working Directory:" try the bin\debug directory
回答4:
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 +
来源:https://stackoverflow.com/questions/44191113/running-a-c-sharp-program-with-command-line-prompts-in-visual-studio-code