Udemy

First .NET Core Console Application using Command Prompt

Monday, November 27, 2017 0 Comments A+ a-

Introduction

In this post, we will create our very first console application in .NET Core, we will see how we can build .NET Core based applications without using any IDE or Visual Studio. We will be using Command Line to create, build and run the application. If we don’t need all the fancy features that Visual Studio and Visual Studio Code offers, then we can build a .NET Core application with just a note pad, the only thing we would need is the .NET Core SDK installed on the machine, and all other actions that we do with Visual Studio all can be invoked using the CLI which we will see how it can be done.


Checking .NET Core Installation

Before creating the .NET Core project, it is obvious thing that we should have .NET Core installed on our machine which can be checked by opening a Command Prompt Window or Powershell window and typing in it the dotnet command, if the .NET Core is not already installed on the machine we will get a error as it will not be able to recognize the command:

Installing .NET Core SDK on Windows

As .NET Core is  all about cross platform, so the SDK is available for different platforms which includes Windows 32-bit and 64-bit, MacOS and Linux, if we go to the official download link we can see multiple options available from which can choose as per our need or convenience.


The following is the list taken from official .NET Core download link :


.NET Core List.PNG


We will be creating the application using Windows so let’s download the sdk installer executable for Windows, i downloaded the Installer of Windows (x64) as on my machine i have 64-bit Windows installed.


.NET SDK x64.png
After downloading the installer, proceed to install the SDK :





This installer will install all the necessary components need to develop .NET Core application and running it which includes .NET Core RunTime, SDK and other things as well. The installation will take 2 - 3 minutes to do the installation, and if all the things go right in installation you will see the following window acknowledging the successful installation :



As we were able to install the SDK successfully, now open the command prompt again and type dontnet command again, i am using powershell for this and executing the command gives you back some output that means .NET Core is being setup correctly:




.NET Core CLI provides different commands to create new project, build, clean and all other commands that we normally invoke using Visual Studio, there is a complete list of commands documented on the official documentation page and all the commands can be seen at this link.

Creating Console Application

Now let’s create the simplest famous Hello World console application using command prompt in .NET Core. If all the previous steps have been completed correctly, open the Command Prompt and create a new directory which will contain the source code for the application.


Write the following command on cmd to create the directory:


mkdir First DotNetCoreApp




Now open the directory using the following command:


cd First DotNetCoreApp




Fromt the above image we can verify that we have opened the correct directory.


Adding Project Template

.NET Core comes with it’s own CLI tools enables to create new project using commands in Command Prompt or Powershell without even opening up the IDE either Visual Studio or VS Code.


We will type dotnet new on command line and press enter which will list down all the templates that can be created using this command.


After running the command, we will see few things listed which includes the different flags which are available to do different things which are also available within Visual Studio but the support is added via Command Line too which is great.




If we go little down, we can see all the templates listed which are available via CLI tools:




Now let’s run the command for creating a new Console Application, so write dotnet new and press enter key and this will create new Console Application project in our working directory:




Our project has been created successfully, but to double check and make sure the command worked fine, we can list the directory content and we should be able to see csproj, Program.cs and other files for the application.




Now let’s run the application by executing the dotnet run command in CMD:




One thing to remember here is that if you are creating the project using .NET Core 1.0 SDK, then before the dotnet run command you would need to execute the dotnet restore command which will restore all the NuGet package dependencies of the project, but for .NET Core 2.0 we don’t need to execute this command as calling the dotnet run makes sure to restore the NuGet package dependencies before running the code.


In actual the restore command was called when we execute the command dotnet new and NuGet packages were restored that time, but it is also called on run as well, which can be verified from this GitHub announcement .

Resources