Ramkarthik Krishnamurthy

My Most Frequently Used Dotnet Command-Line Interface (CLI) Commands

As a .NET developer, I spend a huge amount of time working on .NET projects both at work and at home. At work, I have a Windows machine and hence use Visual Studio to do every step in the development process (like creating project, running tests etc). But when I work on anything at home, I use a MacBook Air. While there is Visual Studio Community edition for Mac, I find it extremely slow and that’s where Dotnet Command-Line Interface (CLI) comes to the rescue.

Wait a minute, did I just say macOS and .NET in the same sentence? Yes, I did.

ASP dotnet core is the best thing to happen for a C# developer because it lets you build applications on macOS or Linux that can be run pretty much anywhere (even Raspberry Pi).

Now if you don’t know what dotnet CLI is, you can read more about it here.

Dotnet CLI and VS Code together make it extremely easy to build apps on macOS and Linux. You don’t have to remember many commands. Over the past two years of using dotnet CLI, I found that there are few commands that I constantly use. I Google the rest.

Here are my most frequently used dotnet CLI commands…

Creating a solution:
dotnet new sln -n MySolution -o MySolution
‘-n’ flag is used to specify the name for your solution and ‘-o’ flag will place the output in this new folder.

Creating a project:
dotnet new console -o MyConsoleApp
Console creates a console application. You can use ‘webapp’ for Web Application, ‘webapi’ for Web API, ‘classlib’ for Class Library and so on. You can run ‘dotnet new –help’ to know about other project types that you can create.

Adding project to a solution:
dotnet sln MySolution.sln add MyConsoleApp/MyConsoleApp.csproj
The above command should be run from inside your solution folder.

Adding project reference to another project:
dotnet add MyConsoleApp/MyConsoleApp.csproj reference MyClassLibrary/MyClassLibrary.csproj
Again, this command should be run from inside your solution folder. If you are running it from your ConsoleApp folder, you will have to make necessary changes to the relative location of both .csproj files.

Build a project:
dotnet build

Run a project:
dotnet run

Restore a project:
dotnet restore

And now for testing (MS Unit/NUnit/XUnit etc)…

Run all test:
dotnet test
You will be running this from the test project folder.

Run a specific test:
dotnet test –filter FullyQualifiedName=MyNamespace.MyClass.MyMethod

Run tests from a specific class:
dotnet test –filter FullyQualifiedName~MyNamespace.MyClass

Run specific category of tests:
MSTest: dotnet test –filter TestCategory=CategoryName
XUnit: dotnet test –filter Category=CategoryName
The difference here is because of the difference in how you setup categories for different frameworks. MSTest uses “TestCategory” whereas XUnit uses “Trait”.

Most of the above commands have many different flags that you can set but I rarely use them. This makes it easy to remember all the commands that I do use frequently.

Your productivity will improve significantly once you learn these dotnet CLI commands. Especially if you use Mac or Linux.