DEV Community

Cover image for The DotNet CLI Commands 🖥️
Rasheed K Mozaffar
Rasheed K Mozaffar

Posted on

The DotNet CLI Commands 🖥️

The DotNet CLI or the Command Line Interface is a very powerful tool that comes with .Net , and as a .Net developer , you should be comfortable using the CLI , cause it'll help you speed up your development , additionally , it's an essential tool in case you are using a different IDE than the beefy Visual Studio which provides most of what the CLI can offer in a well organized , easy to use User Interface (UI).

NOTE !

Before you continue reading this article , make sure to have the latest .Net SDK downloaded , if not the latest , at least one of the .Net Core SDKs , otherwise the CLI won't be available for you to use , if you have it installed already , use dotnet --version to check the current version of the .Net SDK you're running , if that doesn't work , then head to this link to download the SDK https://dotnet.microsoft.com/en-us/download , after downloading the SDK , you can run this previous command and you should see the version of the SDK you downloaded. Now with that said , you can continue !

Let's start with a definition.

.Net Command-line-interface or (CLI) for short , is a tool that allows developers to execute commands for building , running , testing and publishing .Net Application , in addition , the CLI is cross-platform , which implies that the commands written on Windows also apply on MacOs and Linux.

The CLI also provides commands for adding/removing packages , referencing other .Net projects , creating new projects ,and much more , so as you can already tell , the CLI is extremely versatile and worth investing the time in.

With that being said , let's start writing some commands !

Beginning To Use The CLI.

If you use Visual Studio , then you already have a huge UI to create projects , but we are here for the commands , so fire up your Terminal , and let's explore the commands used for initializing projects.

1: Showing All The Available Project Templates


dotnet new

Enter fullscreen mode Exit fullscreen mode

By typing this command , the CLI will present to you all the available project templates like MVC , Web Api , Console Application , BlazorWasm and BlazorServer just to name a few.

2: Creating a new Console Application


dotnet new console

Enter fullscreen mode Exit fullscreen mode

This command creates a new Console Application within the active Directory , notice this project will take the name of the directory . In case you want a specific name , you can add some options , the name option for instance , like this:


dotnet new console -n/--name MyConsoleApp

Enter fullscreen mode Exit fullscreen mode

Please note that you should only use one of the options,
either -n or --name, depends on which one you prefer , I just added both of them so we don't stretch this command over multiple blocks

If you want further help on the options available for new command if you want to new up projects , use this command:


dotnet new -h/--help

Enter fullscreen mode Exit fullscreen mode

This commands displays all the existing options for this dotnet new command , it shows a template for how the options and configurations are applied on the commands , and it associates each option with an explanation

If you want to view everything from the .Net CLI , you can use this command:


dotnet -h/--help

Enter fullscreen mode Exit fullscreen mode

Another command related to creating new projects , let's say you don't just want only to give the project a specific name , but you also want the new project to go inside a new directory , you can use the -o/--ouput option to generate the new project inside a new directory , to do that , use the following command:


dotnet new console -n MyConsoleApp -o ConsoleApps.

Enter fullscreen mode Exit fullscreen mode

OK , enough of creating projects , let's talk about some commands that help us run , build and test.

2: Running A .Net Project

To run a project , simply use this command inside your project's directory:


dotnet run

Enter fullscreen mode Exit fullscreen mode

This command will run your project , it can be a project of any type , this command will run it

3: Build A .Net Project

This command will build the project so you can check to see if it builds successfully and in case there are any build errors , the terminal will prompt them.


dotnet build

Enter fullscreen mode Exit fullscreen mode

4: Run Unit Tests In A .Net Project

This command will execute the unit tests in case you're running some unit tests , it'll report what tests passed and what tests failed.


dotnet test

Enter fullscreen mode Exit fullscreen mode

5: Add A Package To A .Net Project

You need many packages through the development of your .Net apps , an example could be Entity Framework Core so you can add Database and Data-Access related functionality to your project , to add a package , you use this command:


dotnet add package Microsoft.EntityFrameworkCore

Enter fullscreen mode Exit fullscreen mode

Microsoft.EntityFrameworkCore is just here for demonstration purposes , just to clarify how this command is used , the template of it is like this

dotnet add package [package name]

You would definitely find yourself using this command many times during the initial stages of developing an application cause such packages don't come included by default , if you are using Visual Studio , you can alternatively use NuGet Package Manager (NPM) , which provides a fully fledged UI for installing packages into your project.

There is another variation of this command for removing packages , which looks like this dotnet remove package [package name]

If you want to reference a separate .Net Project , like a class library for example , this one is easy too but a tricky part is specifying the path for the other .Net project.

Go inside the main project , it could be a console app , we'll assume there's a class library project and we want to reference it from within the console app , we'll call our imaginary class library RandomLibrary, the reference command will be like this:


dotnet add reference ../RandomLibrary/RandomLibrary.csproj

Enter fullscreen mode Exit fullscreen mode

Make sure you add this command while you're inside the console project directory.

NOTE: We have this ../ in our path , this means the following , move back a directory , because our class library isn't inside the same directory as the console app , so we wanna move back , it's like clicking the back arrow in a browser to move back to the previous tab.

Well what about tools ? Is there anything we can use to add/remove tools ? Heck yeah , we have a list of commands for tools , that's what we will cover next.

6: Add/Remove Tools From A .Net Project

You might want to use some tools like Entity Framework Core tool or dotnet ef , this tool allows you to write commands to add/remove migrations , update the database , and roll back to a certain snapshot for the database.

To add a tool , use this command:


dotnet tool install [tool name]

Enter fullscreen mode Exit fullscreen mode

You can use the --global flag to make the tool available for the user from anywhere on the computer , in case you don't add this flag , the tool will default to the --local flag which makes it only available from within the project files.

You can add the --global flag like this:


dotnet tool install [tool name] --global

Enter fullscreen mode Exit fullscreen mode

If you wanna remove a tool , use this command:


dotnet tool uninstall [tool name]

Enter fullscreen mode Exit fullscreen mode

Again , the global flag is viable here , if you want to uninstall the tool globally.

7: Listing Downloaded Packages In A Project

Often times , you may wanna view the downloaded packages in your project , check their versions or to see if there's any unused package so you can remove it, to do that , use the list command , like this:


dotnet list package

Enter fullscreen mode Exit fullscreen mode

This command will output all the packages in your project , with their respective versions.

8: Listing Referenced Project By A .Net Project

We talked about referencing other projects previously in this article , when your application expands , it might be referencing multiple other projects or libraries , to view the references , use this command:


dotnet list reference

Enter fullscreen mode Exit fullscreen mode

WOHOOO

You learned many CLI commands today , make sure to play around with them , make some mistakes and learn from them , but also be accurate when typing these commands, cause in long commands like adding packages with many words in them , it's more likely than ever you'll make a spelling mistake , and you won't get any specific error about that spelling mistake you committed , so always double check the spelling.

This article covered many commands and I showed you how to view the rest of them in case you want any help with a specific command , you can always call the -h/--help options and the CLI will provide instructions for you , in case you wanna check all the available commands the CLI has , you can check out this article here:

https://learn.microsoft.com/en-us/dotnet/core/tools/

For now , you can use the CLI with some confidence , you already have some commands up your sleeve , you'll look impressive when you use them. 😂

GoodBye Now !

Top comments (0)