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.

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”.

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

HostForLIFE.eu ASP.NET Core Hosting

European best, cheap and reliable ASP.NET hosting with instant activation. HostForLIFE.eu is #1 Recommended Windows and ASP.NET hosting in European Continent. With 99.99% Uptime Guaranteed of Relibility, Stability and Performace. HostForLIFE.eu security team is constantly monitoring the entire network for unusual behaviour. We deliver hosting solution including Shared hosting, Cloud hosting, Reseller hosting, Dedicated Servers, and IT as Service for companies of all size.