CLI commands in .NET

Visual Studio is an intuitive environment for managing our projects that is used extensively by

.NET workers and is very popular with developers. But we do not have access to this powerful IDE everywhere. .NET CLI commands help us manage our projects without using Visual Studio. In the continuation of this article, we will get acquainted with how to use them.

CLI stands for Command Line Interface, which is a mechanism for communicating with operating systems and applications and executing specific commands by typing commands. We .NET works typically use the graphical and visual environment of Visual Studio. (The word "Visual Studio" itself means visual environment πŸ˜€)

In the Visual Studio environment, we can do a variety of things, such as creating a project or a new solution.

Well, now, for example, you want to write a project on the spot that is not very small, but for example needs a package, or elsewhere (you get it!) You need to use Visual Studio. Because Visual Studio One cannot be installed on every system, our work gets stuck there.

This is where we get to the command line. Learning command line commands for .NET can help you make better use of .NET.

For example, in the Linux operating system we can not use Visual Studio, but there we have a great VSCode editor. Well VSCode has a terminal that executes exactly the same commands (something like Windows Power Shell!) And we can easily create our own projects, debug, output and ...

I want to remind you of the most widely used and useful CLI .NET commands.

Before that we need to have .Net Core. .Net 5.0 will be great if you install.

You can use this link to download

When we create a new project in Visual Studio. First, Visual Studio creates a solution for us, and then it creates a project and throws that project into the solution that it created, and then we make a change on that project, and then we build our project and that. We run and test and debug and then release it as a release, and here the software development process ends. It remains until we want to update the method again later and when we come again and open that solution, we change and ...

Now let's do the same with CLI!

Well, to get started, we create a folder and name a method (preferably a project name). We use the name SayHello here. (We want to write a program that receives a name from the user (for example Ali) and tells the user Hello Ali)

Now we open the Power Shell and here begins the fascinating and beautiful process of grammar.

We need a solution, but we must first go to the location of the folder we created. There are two ways to do this. The first way is to hold down the Shift key and right-click on the folder and select Open Power Shell Window Here:


Now Windows PowerShell opens in our folder location

  But there is another way is to go to our folder with the cd command:


Now we are in our own folder. And here we have to order .NET. To create a solution, we use the following command:


This will create a solution in the folder we created. The new solution is the same name as the folder.  

By entering the above command, you will see something like this:


If you go to the folder location you will see an empty solution with the same name as the folder created and we do not have a project in this solution yet:

If you want the created solution to have another name, you can write the following command:


Here we have to create a new project.

The dotnet new command is for creating something new. Now it can be an app console, a solution (which we created above to create a new solution) and ...

Here is a table that shows us what we can create with dotnet new. Just type the short name of each one you want in front of the dotnet new command:


TemplatesShort nameLanguageTagsIntroduced
Console Applicationconsole[C#], F#, VBCommon/Console1.0
Class libraryclasslib[C#], F#, VBCommon/Library1.0
WPF Applicationwpf[C#], VBCommon/WPF3.0 (5.0 for VB)
WPF Class librarywpflib[C#], VBCommon/WPF3.0 (5.0 for VB)
WPF Custom Control Librarywpfcustomcontrollib[C#], VBCommon/WPF3.0 (5.0 for VB)
WPF User Control Librarywpfusercontrollib[C#], VBCommon/WPF3.0 (5.0 for VB)
Windows Forms (WinForms) Applicationwinforms[C#], VBCommon/WinForms3.0 (5.0 for VB)
Windows Forms (WinForms) Class librarywinformslib[C#], VBCommon/WinForms3.0 (5.0 for VB)
Worker Serviceworker[C#]Common/Worker/Web3.0
Unit Test Projectmstest[C#], F#, VBTest/MSTest1.0
NUnit 3 Test Projectnunit[C#], F#, VBTest/NUnit2.1.400
NUnit 3 Test Itemnunit-test[C#], F#, VBTest/NUnit2.2
xUnit Test Projectxunit[C#], F#, VBTest/xUnit1.0
Razor Componentrazorcomponent[C#]Web/ASP.NET3.0
Razor Pagepage[C#]Web/ASP.NET2.0
MVC ViewImportsviewimports[C#]Web/ASP.NET2.0
MVC ViewStartviewstart[C#]Web/ASP.NET2.0
Blazor Server Appblazorserver[C#]Web/Blazor3.0
Blazor WebAssembly Appblazorwasm[C#]Web/Blazor/WebAssembly3.1.300
ASP.NET Core Emptyweb[C#], F#Web/Empty1.0
ASP.NET Core Web App (Model-View-Controller)mvc[C#], F#Web/MVC1.0
ASP.NET Core Web Appwebapp, razor[C#]Web/MVC/Razor Pages2.2, 2.0
ASP.NET Core with Angularangular[C#]Web/MVC/SPA2.0
ASP.NET Core with React.jsreact[C#]Web/MVC/SPA2.0
ASP.NET Core with React.js and Reduxreactredux[C#]Web/MVC/SPA2.0
Razor Class Libraryrazorclasslib[C#]Web/Razor/Library/Razor Class Library2.1
ASP.NET Core Web APIwebapi[C#], F#Web/WebAPI1.0
ASP.NET Core gRPC Servicegrpc[C#]Web/gRPC3.0
dotnet gitignore filegitignore Config3.0
global.json fileglobaljson Config2.0
NuGet Confignugetconfig Config1.0
Dotnet local tool manifest filetool-manifest Config3.0
Web Configwebconfig Config1.0
Solution Filesln Solution1.0
Protocol Buffer Fileproto Web/gRPC3.0

The general structure of .NET CLI commands is as follows:

1. the word dotnet  

2. Command (for example, creating a new solution (dotnet new sln))  

3. Arguments  

4. Settings (for example, in what language .NET languages ​​(such as C #, F #, VB) are made). 

Or in other words 


: dotnet <command> <argument> <options>  

Now let's move on to our job of building a console project. As you know, the following command is used to build a console project:

dotnet new console

Still waiting!

Now we have to give him the options of this console project.

If you press like this and the command is executed, it will create a project with the default settings.

But I want you to learn its settings so that you can be full.  

If you type h in front of the dotnet new console command, you will encounter something like this:



It actually tells us all the settings for a project that we do not need all of them

We now only need a few important options that are widely used

N- or name- option:

This option is to put a name on the project or file we want (like the same command we wrote to create a solution with the desired name!)

-Lang or –language option:

This option is to set the programming language of the project, which is selected from C #, F # and VisualBasic languages:

In the above command, we chose our language as Basic Basic (do not worry, I basically want to work in C #). To do this, abbreviate the word Visual Basic, which can be VB in two double quotes

Well, we told these two to go to the main task of creating a C # project and ...

Do you know what command to write:

Now we have a project called sayhello-app that is going to be written in C #.

Now let's take a look at the first folder we created and see that we have a folder called sayhello-app (the name of the project we created):

Let's take a look inside the sayhello-app folder:

Look at their types.  

Exactly like the projects we make with Visual Studio.  

Now we need to add our project to the solution (the same solution we created).  

To do this, we use the following command:

Well, let me explain the above command to you

You know the first part (dotnet sln) that we worked on. The word add we said was to add a project to our solution.

Then we came up with the name of the project and put a back slash (/) to separate it from the next argument.

The next argument is the project file path (stored in csproj in cspro, fsproj in f # and vsproj in Visual Basic. This file contains general project information), which is measured relative to the location of the solution.

Now our project has been added to the solution and we can open this solution with the studio itself.

But the time for programming has come

(We did all this work, but know that when you learn this, you can create and manage a project faster than Visual Studio itself 😁)

Open the Program.cs file (Program.fs in F Sharp and Program.vb in Visual Basic) with an editor code (it can be done with Windows Notepad, but I use VSCode to show you the terminal).

If you want to work with vscode, first right-click on your project folder and select Open with Code:

Now Pushmon runs with Visual Studio code:


We enter the program file and well, here he has given us back things as an example. Because maybe the person reading this article is not C # and I do not want to learn C#, I do not say why, but we write the same code:


Now it's time to run our own program.

Our friends used to give our instructions to .NET with the power shell of Windows. But vscode has thought of everything and put a terminal that does exactly what Power Shell does, but here it is, and I, as a programmer, say, "It makes my job a lot easier and faster!"

Now let's go to the terminal

To create a new terminal, select the New Terminal option from the Terminal menu to create a new terminal for us:

Now we write our commands directly in the terminal:


To run a project, go to the folder where the project file is located and write the dotnet run command:


And now the program is running and we have to name it so that it can greet us:

The program exe file is also located in the \SayHello\sayhello-app\bin\Debug\net5.0 folder:

If you want to set what framework to run, use f-:

This table is also for CLI framework names:


Target FrameworkTFM
.NET 5 (and .NET Core)netcoreapp1.0
netcoreapp1.1
netcoreapp2.0
netcoreapp2.1
netcoreapp2.2
netcoreapp3.0
netcoreapp3.1
net5.0*

The friends of this article were mostly familiar with CLIs in .NET. Take a look at your Microsoft articles to learn more.  

 

I hope you are successful and your plans do not bug πŸ˜‰







User:ToProgramming
1/1/2021
Visit:38
CSharp

Commands Power Shell .Net dotnet Core C# CLI command line
You must be logged in to post a comment