Creating A .NET Application From The CLI

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.

Creating A .NET Application From The CLI

First, we create a folder for our application and move to that folder. Here we type “dotnet new console” to create the console application.

Creating A .NET Application From The CLI

Creating A .NET Application From The CLI

Next, we will create the solution using “dotnet new sln”.

Creating A .NET Application From The CLI

Now, we will add this project to the solution using “dotnet sln TestApp.sln add TestApp.csproj”.

Creating A .NET Application From The CLI

We now want to modify the “Program.cs” file. For this, we will use notepad. Run the “notepad Program.cs” command.

Creating A .NET Application From The CLI

Creating A .NET Application From The CLI

Modify, the code as below.

Creating A .NET Application From The CLI

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.

Creating A .NET Application From The CLI

Finally, we run the code using “dotnet run”. Here we enter a string, and it is displayed to us with the Prefix “Hello from”.

Creating A .NET Application From The CLI

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.

Creating A .NET Application From The CLI

When I ran it for the first time, I got the below error.

Creating A .NET Application From The CLI

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

Creating A .NET Application From The CLI

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!