Introduction
In today’s article, we will take a look at creating a .NET application from the command line interface (CLI). In the past, I have always created applications using Visual Studio. Today, we will create a simple console application using only the CLI and notepad. Finally, we will open this application in Visual Studio to verify all is compatible. So, let us begin.
Creating the .NET application
The first step is to open the Command Prompt for the Visual Studio instance or simply open the command line. I would prefer to use the “Run as Administrator” option.
First, we create a folder for our application and move to that folder. Here we type “dotnet new console” to create the console application.
Next, we will create the solution using “dotnet new sln”.
Now, we will add this project to the solution using “dotnet sln TestApp.sln add TestApp.csproj”.
We now want to modify the “Program.cs” file. For this, we will use notepad. Run the “notepad Program.cs” command.
Modify, the code as below.
Code is below,
var myName = Console.ReadLine();
Console.WriteLine($"Hello from {myName}");
We are now ready to build and run the code. Run “dotnet build” to build the code.
Finally, we run the code using “dotnet run”. Here we enter a string, and it is displayed to us with the Prefix “Hello from”.
Testing the application in Visual Studio 2022 community preview edition
Let us verify that the solution and project we just created works with Visual Studio. We will open the same solution using Visual Studio 2022 community preview edition.
When I ran it for the first time, I got the below error.
To fix this, I simply opened the project file, made a small change (adding a space and then removing it), and saved it. After that, all worked fine
Summary
In this article, we took a look at creating a .NET application from the CLI without using any tool. As we saw it is a simple process and the advantage of this is that it can be automated in scripts. We also opened this solution in Visual Studio and confirmed all is working fine. Happy coding!