How to create dotnet core WinForms Application 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 a dotnet core WinForms (Windows Form) application, WPF (Windows Presentation Foundation) application, console application, Web 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 a “dotnet core WinForms (Windows Forms) Application”

The user can use the dotnet new “Windows Forms” or “dotnet new winforms” command from the dotnet-CLI toolset to create a new dotnet core WinForms (Windows Forms) application. 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 winforms –no-restore”: This command does not restore the required project dependencies by the WinForms application. Please follow through to read about a few “dotnet new winforms” command usage.

Prerequisite:

.NET Core SDK 3.0 (preview) and above. The current version is .NET Core 5.0 and can be downloaded here.

Command Syntax:

The format of the command is as follows: dotnet new winforms [options]

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

C:\>dotnet new winforms --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.


Windows Forms (WinForms) Application (C#)
Author: Microsoft
Description: A project for creating a .NET Core Windows Forms (WinForms) Application
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 winforms:

1. “dotnet new winforms”

The command creates a new “NET Core Windows Forms” project. The name of the project is not specified in the command so by default the project name will be the same as the directory where the command was executed, additionally, the command will also restore the dependency required by the project implicitly. The default language is C#, so the project created will be of C# type. The list of the file that will be created using the mentioned commands is given below.

dotnet new winforms
C:\Temp>dotnet new winforms
The template "Windows Forms (WinForms) Application" was created successfully.

Processing post-creation actions...
Running 'dotnet restore' on C:\Temp\Temp.csproj...
  Restore completed in 162.66 ms for C:\Temp\Temp.csproj.

Restore succeeded.

Files and folders created by the command:

C:\temp>dir
 Volume in drive C is OSDisk
 Volume Serial Number is 8053-FAC8

 Directory of C:\temp

06/17/2019  05:06 PM    <DIR>          .
06/17/2019  05:06 PM    <DIR>          ..
06/17/2019  05:06 PM               387 Form1.cs
06/17/2019  05:06 PM             1,234 Form1.Designer.cs
06/17/2019  05:06 PM             5,817 Form1.resx
06/17/2019  05:06 PM    <DIR>          obj
06/17/2019  05:06 PM               527 Program.cs
06/17/2019  05:06 PM               242 temp.csproj
06/17/2019  05:06 PM    <DIR>          xlf

2.”dotnet new winforms –name Hello”

The command creates a new “NET Core Windows Forms” project having the name of Hello.csproj. The command creates a Hello directory only if doesn’t exist and then creates the boilerplate files in the directory, additionally, the command also restores the dependency required by the project.

C:\>dotnet new winforms --name Hello
The template "Windows Forms (WinForms) Application" was created successfully.

Processing post-creation actions...
Running 'dotnet restore' on Hello\Hello.csproj...
  Restore completed in 96.53 ms for C:\Hello\Hello.csproj.

Restore succeeded.

Files and folders created by the command:

C:\>cd Hello

C:\Hello>dir
 Volume in drive C is OSDisk
 Volume Serial Number is 8053-FAC8

 Directory of C:\Hello

06/17/2019  05:09 PM    <DIR>          .
06/17/2019  05:09 PM    <DIR>          ..
06/17/2019  05:09 PM               388 Form1.cs
06/17/2019  05:09 PM             1,235 Form1.Designer.cs
06/17/2019  05:09 PM             5,817 Form1.resx
06/17/2019  05:09 PM               242 Hello.csproj
06/17/2019  05:09 PM    <DIR>          obj
06/17/2019  05:09 PM               528 Program.cs
06/17/2019  05:09 PM    <DIR>          xlf

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

The command creates a new “NET Core Windows Forms” project having the name of Hello. The command creates a Hello directory only if doesn’t exist and then creates the boilerplate files in the directory, the command does not restore the dependency required by the project. The output of the command is as follows:

C:\>dotnet new winforms --name Hello --no-restore
The template "Windows Forms (WinForms) Application" was created successfully.

C:\>cd Hello

C:\Hello>dir
 Volume in drive C is OSDisk
 Volume Serial Number is 8053-FAC8

 Directory of C:\Hello

06/17/2019  05:13 PM    <DIR>          .
06/17/2019  05:13 PM    <DIR>          ..
06/17/2019  05:13 PM               388 Form1.cs
06/17/2019  05:13 PM             1,235 Form1.Designer.cs
06/17/2019  05:13 PM             5,817 Form1.resx
06/17/2019  05:13 PM               242 Hello.csproj
06/17/2019  05:13 PM               528 Program.cs
06/17/2019  05:13 PM    <DIR>          xlf

4. “dotnet new winforms –name Hello –languageVersion 7.0”

The command creates a new “NET Core Windows Forms” project having the name of Hello. The command creates a Hello directory only if doesn’t exist and then creates the boilerplate files and directory, additionally, the command also restores the dependency required by the project. The command creates the project specific to C# 7.0 (version). The details can be found in the Hello.csproj file.

C:\>dotnet new winforms --name Hello --langVersion 7.0
The template "Windows Forms (WinForms) Application" was created successfully.

Processing post-creation actions...
Running 'dotnet restore' on Hello\Hello.csproj...
  Restore completed in 108.97 ms for C:\Hello\Hello.csproj.

Restore succeeded.

Content of Hello.csproj file: LangVersion tag contains 7.0

C:\Temp\dotnet\Hello>type Hello.csproj

<Project Sdk="Microsoft.NET.Sdk.WindowsDesktop">
  <PropertyGroup>
    <OutputType>WinExe</OutputType>
    <TargetFramework>netcoreapp3.0</TargetFramework>
    <LangVersion>7.0</LangVersion>
    <UseWindowsForms>true</UseWindowsForms>
  </PropertyGroup>
</Project>

I hope you found this post on how to create a WinForms Application using dotnet-cli. Thanks for visiting. Cheers !!!

[Further Readings: How to create a dotnet core WPF application using dotnet-cli |  How to create a dotnet core console app using dotnet-cli |  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    ]

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