Using dotnet CLI
Working with .NET using VS-Code, you use the dotnet command line interface quite extensively, with many commands being repeated over and over again.
All the commands start with dotnet. After dotnet, we can supply a command (also known as verb) to perform a specific action, sometimes followed by arguments and option. Below is a list of the more common commands you will both encounter and use.
If you are unsure what will happen when you run a command, the flag –dry-run will show the result of what a command will do, instead of executing it.
Basic commands
Command |
Result |
dotnet run |
Run a .NET project |
dotnet test |
Run the tests for a .NET project |
dotnet restore |
Restor dependencies for the project or solution in current folder |
dotnet build |
Build (and restore) dependencies for the project or solution in current folder |
dotnet clean |
Remove unnecessary files from the build output |
dotnet sdk options
Command |
Result |
dotnet -h |
Show help |
dotnet -info |
Show detailed information about the .NET installation and the machine environment |
dotnet --list-sdks |
Display the current installed SDKs |
dotnet --version |
Display the .NET SDK version in use |
dotnet new - creating new files
Command |
Result |
dotnet new sln |
New solution placed in the current directory with the name of the current directory |
dotnet new sln -n <solution-name> -o <output-directory> |
New solution with name <solution-name> placed inside the <output-directory> , if one argument/flag is missing (-o or -n), the one missing defaults to the supplied argument |
dotnet new <project-template> |
New <project-template> project |
dotnet new <project-template> -n <project-name> -o <output-directory> |
New project with name <project-name> in folder <output-directory> |
dotnet new -l |
Lists installed templates |
dotnet new -h |
Show help |
dotnet new <search-string> --search |
Search for available template packages |
dotnet new --install <PACKAGE_ID> |
Install a new template package |
dotnet new gitignore |
New .gitignore for .NET adapted to - projects |
Example |
Result |
dotnet new classlib -n TestClass -o newDir |
Class library project named TestClass in new folder named newDir |
dotnet new xUnit -o TestClass.Tests |
xUnit test library named TestClass.Tests in new folder |
dotnet new web |
Empty web application named after —and placed in— the current active folder |
dotnet new webapi -n WebProject |
New project named WebProject |
dotnet new webapi -n WebProject --dry-run |
Show what would happen if the above command would run |
dotnet new webapi -n WebProject --force |
same as above, but overwrites the existing WebProject |
dotnet add - adding resources to files
Command |
Result |
dotnet sln add <project> |
Add a project to a solution |
dotnet add <project> reference <otherProject> |
Add a reference to <otherProject> from <project so that <project> can access <otherProject> ’s types |
dotnet add <project> package <nuget-package> |
Add a NuGet package to <project> |
Example |
Result |
dotnet sln add ./MyProject |
Add a project MyProject to the solution in the current folder |
dotnet add ./MyProject reference ./SeparateProject |
Add a reference to SeparateProject in MyProject |
dotnet add ./MyProject package Fluent.Assist.Dynamic |
Add a reference to the NuGet package named Fluent.Assist.Dynamic |
dotnet run - running .NET applications
Command |
Result |
dotnet run |
Run the found project inside either the current folder or solution |
dotnet run -p ./<directory>/<project> |
Run the <project> found in the subfolder <directory> |
dotnet run -c Debug |
Run given a specific configuration, default is Debug |
dotnet test - testing .NET applications
Command |
Result |
dotnet test |
Run the testproject the found inside either the current folder or solution |
dotnet test --nologo -v:q |
Remove logo from output and have minimal test output |
dotnet test -p <testProject> |
Run tests in <testProject> |
dotnet test --filter namespace.class.method |
Run the specific test namespace.class.method |
dotnet test -h |
Show the commands, there are a LOT of flags |
An additional command is the watch command that starts a file watcher running the selected command on file change.
Example |
Result |
dotnet watch run |
Rerun the run command on file update |
dotnet watch test |
Rerun all the tests on file update |
dotnet watch run -p <project> |
Rerun <project> on file update |
You can read more about the dotnet CLI on docs.microsoft.com