How to create a dotnet core console app using dotnet-cli

NET core command-line interface (dotnet-cli) is a new NET Core toolset (commands) for developing NET Core Applications. The dotnet-cli toolset provides a list of pre-installed dotnet core project templates from which the user can create various applications as the dotnet core Console app, Web application, WPF (Windows Presentation Foundation) application, Windows Form application, and other test applications. The application created with dotnet-cli can be executed on any Windows, Linux, or macOS operating system.

How to create “dotnet core console app” using dotnet new command

The user can use the dotnet new “Console Application” or “dotnet new console” command from the dotnet-CLI toolset to create a command-line application (dotnet core console app). The command provides a few options that can be applied as a parameter to tweak the output of the command as shown below :

“dotnet new console –no-restore”: This command does not restore the required project dependencies by the console application. Please follow through to read about a few “dotnet new console” command usage.

Prerequisite:

NET Core SDK 1.x and above. The current version is NET Core 5.0, and can be downloaded here:

Command Syntax:

The syntax of the command is as follows: dotnet new console [options]

The details of all the available command options can be listed using the –help option as follows:

C:\>dotnet new console --help
Usage: new [options]

Options:
  -h, --help          Displays help for this command.
  -l, --list          Lists templates containing the specified name. If no name is specified, lists all templates.
  -n, --name          The name for the output being created. If no name is specified, the name of the current directory is used.
  -o, --output        Location to place the generated output.
  -i, --install       Installs a source or a template pack.
  -u, --uninstall     Uninstalls a source or a template pack.
  --nuget-source      Specifies a NuGet source to use during install.
  --type              Filters templates based on available types. Predefined values are "project", "item" or "other".
  --dry-run           Displays a summary of what would happen if the given command line were run if it would result in a template creation.
  --force             Forces content to be generated even if it would change existing files.
  -lang, --language   Filters templates based on language and specifies the language of the template to create.


Console Application (C#)
Author: Microsoft
Description: A project for creating a command-line application that can run on .NET Core on Windows, Linux and macOS
Options:
  --langVersion  Sets langVersion in the created project file
                 text - Optional

  --no-restore   If specified, skips the automatic restore of the project on create.
                 bool - Optional
                 Default: false / (*) true


* Indicates the value used if the switch is provided without a value.

Command Usage – dotnet new console:

1. “dotnet new console”

The above command is used to create a new “dotnet core console application”. As the name of the application is not specified in the command so the project will be created by the name of the directory the command is executed in it. This command also performs an implicit restore of dependencies required for the project.

dotnet new console
C:\temp>dotnet new console
The template "Console Application" was created successfully.

Processing post-creation actions...
Running 'dotnet restore' on C:\temp\temp.csproj...
  Restoring packages for C:\temp\temp.csproj...
  Generating MSBuild file C:\temp\obj\temp.csproj.nuget.g.props.
  Generating MSBuild file C:\temp\obj\temp.csproj.nuget.g.targets.
  Restore completed in 141.06 ms for C:\temp\temp.csproj.

Restore succeeded.

2. “dotnet new console –name “Hello”

The command creates a “Hello” directory only if doesn’t exist and then creates a “dotnet core console application” in it, the name of the project file is “Hello.csproj”. The command also performs an implicit restore of dependencies required for the project.

C:\>dotnet new console --name "Hello"
The template "Console Application" was created successfully.

Processing post-creation actions...
Running 'dotnet restore' on Hello\Hello.csproj...
  Restoring packages for C:\Hello\Hello.csproj...
  Generating MSBuild file C:\Hello\obj\Hello.csproj.nuget.g.props.
  Generating MSBuild file C:\Hello\obj\Hello.csproj.nuget.g.targets.
  Restore completed in 157.63 ms for C:\Hello\Hello.csproj.

Restore succeeded.

3. “dotnet new console –name “Hello” –no-restore”

The command creates a “Hello” directory only if it doesn’t exist and then creates the “dotnet core console application” in it, the name of the project file is “Hello.csproj”. The command does not perform any restore operation and create only two files viz: Hello.csproj and Program.cs. The content of the Hello directory is given below

C:\>dotnet new console --name "Hello" --no-restore
The template "Console Application" was created successfully

C:\Hello>dir
 Volume in drive C is Windows
 Volume Serial Number is 945D-A84B

 Directory of C:\Hello

06/12/2019  10:17 PM    <DIR>          .
06/12/2019  10:17 PM    <DIR>          ..
06/12/2019  10:17 PM               178 Hello.csproj
06/12/2019  10:17 PM               187 Program.cs
               2 File(s)            365 bytes
               2 Dir(s)  42,173,321,216 bytes free

I hope you find the post helpful. Thanks for visiting. Cheers!!!

[Further Readings: Top 7 Web Frameworks to Learn and Focus on in 2021 |  Top 7 Programming Languages to Focus on in 2021 |  Creational Design Patterns |   Abstract Factory Design Pattern in C#  |  Factory Method Pattern in C#  |  Singleton Design Pattern in C#  |  Introduction to Design Patterns  |  How to add Git Bash to Windows Terminal Application  |  How to customize Windows Terminal Application  |  How to customize Windows Terminal Key Bindings  |  How to use Blazor EditForm for Model Validation using C#  |  How to publish a Blazor Server Application to IIS  ]

5 2 votes
Article Rating
Subscribe
Notify of
guest
2 Comments
oldest
newest most voted
Inline Feedbacks
View all comments
2
0
Would love your thoughts, please comment.x
()
x