Creating a Windows Forms Application in C# using `dotnet new`
Starting from dotnet 3.0 you can just run the following command for initializing WinForms Application:
dotnet new winforms
For initializing wpf application just run:
dotnet new wpf
You can see all available project types for dotnet 3.0 by running dotnet new
or dotnet new --help
(both commands produce the same output).
P.S.: tested with dotnet 3.0.100-preview-010184
.
This answer is a workaround. And a hacky workaround at that. If you can, use Visual Studio instead of this.
It took a bit (read: lot) of puzzling, but I managed to tie some bits of info left and right together.
Creating Forms, which framework?
According to this answer on another question, there are different frameworks within .NET which allow the creation of different apps, as shown by this graphic:
Another post on Quora seems to second this point:
All native user interface technologies (Windows Presentation Foundation, Windows Forms, etc) are part of the framework, not the core.
This means that while we are using the wrong framework. By default, dotnet new
seems to use the .NET CORE framework, as we can see in the .csproj file:
<TargetFramework>netcoreapp2.0</TargetFramework>
This is not what we want. We want the .NET FRAMEWORK. According to the Microsoft Documentation we can change this to net<versionnumber>
.
Adding the dependency
The dependency System.Windows.Forms
can then be added like so:
<PackageReference Include="System.Windows.Forms" HintPath = "\..\WINDOWS\Microsoft.NET\Framework\v4.0.30319"/>
One last thing
When compiling this, I ran into another compilation error:
error CS0656: Missing compiler required member 'Microsoft.CSharp.RuntimeBinder.CSharpArgumentInfo.Create'
Which can be easily fixed by adding Microsoft.CSharp
to the dependencies using NuGet
.
The .csproj file then looks like this:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net452</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Newtonsoft.Json" Version="10.0.2"/>
<PackageReference Include="System.Windows.Forms" HintPath="\..\WINDOWS\Microsoft.NET\Framework\v4.0.30319"/>
<PackageReference Include="Microsoft.CSharp" Version="4.4.0"/>
</ItemGroup>
</Project>