Click here to Skip to main content
13,628,146 members
Click here to Skip to main content
Add your own
alternative version

Stats

7.8K views
9 bookmarked
Posted 6 Dec 2017
Licenced CPOL

Building First Console Application using Command Prompt in .NET Core 2.0

, 8 Jan 2018
Rate this:
Please Sign up or sign in to vote.
This is a step by step guide for beginner on how to install .NET Core 2.0 and create a Console Application without any IDE using Command Prompt/PowerShell

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 can be invoked using the CLI. We will see how it can be done.

Checking .NET Core Installation

Before creating the .NET Core project, it is an 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 an error as it will not be able to recognize the command:

Installing .NET Core SDK

As .NET Core is all about cross platform, 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 we can choose as per our need or convenience.

The following is the list taken from the 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 I have 64-bit Windows installed on my machine.

.NET SDK x64.png

After downloading the installer, proceed to install the SDK:

This installer will install all the necessary components needed 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 dotnet 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

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

Adding Project Template

.NET Core comes with its own CLI tools that enable to create a 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 slightly 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 a 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.

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

Summary

We saw that how we can create, build and run .NET Core based application using either Command Prompt or PowerShell, the only thing that we need to start developing the application without IDE is to install the .NET Core SDK on the machine and we are done. 

Resources

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Ehsan Sajjad
Software Developer
Pakistan Pakistan
Ehsan Sajjad is a Microsoft Certified Professional, Microsoft Certified C# Specialist and he is also among the top users on StackOverflow from Pakistan with 39k+ reputation at time of writing this and counting.

He is a passionate software developer with around 5 years of professional experience in Microsoft Technologies both web and desktop applications and always open to learn new things and platforms especially in mobile application development and game development.

Some Achievements :

  • 5th Top Contributor from Pakistan on Stackoverflow.com
  • Top User in ASP.NET MVC from Pakistan on Stackoverflow.com
  • 21st June 2017 - Article of the Day - ASP.NET Community (Beginners Guide on AJAX CRUD Operations in Grid using JQuery DataTables in ASP.NET MVC 5)
  • 19th April 2017 - Article of the Day - ASP.NET Community (ASP.NET MVC Async File Uploading using JQuery)
  • March 2017 - Visual C# Technical Guru Silver Medal on Microsoft Tech Net Wiki Article Competition
  • 20 January 2017 - Article of the Day - ASP.NET Community (Async File Uploading in ASP.NET MVC)
  • 22nd September 2016 - Article of the Day - ASP.NET Community (GridView with Server Side Filtering, Sorting and Paging in ASP.NET MVC 5)
  • 22nd August 2016 - Article of the Day - ASP.NET Community (Beginners Guide for Creating GridView in ASP.NET MVC 5)
  • December 2015 - C-SharpCorner Monthly Winner

You may also be interested in...

Pro
Pro

Comments and Discussions

 
-- There are no messages in this forum --
Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web03 | 2.8.180712.1 | Last Updated 8 Jan 2018
Article Copyright 2017 by Ehsan Sajjad
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid