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

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 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 Example:

1. “dotnet new console”

This 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 core console app, NET Core, dotnet-cli, dotnet new console app
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.

Read About: Simple steps to changes Visual Studio 2019 Themes

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

Further readings:

Read about: How to display installed dotnet core project templates and create dotnet core library

Download the latest version of NET Core or refer MSDN for further details

0 0 vote
Article Rating
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments