Creating a Windows Forms Application in C# using `dotnet new`

6

I am trying to create a (simple) windows form application in Visual Studio Code 2017. Sadly I am unable to use the full VS versions, so I don't have the menu options to create new projects.

Previously, I have created console projects by running dotnet new console from the terminal in VSC. However, I can't get this to work with a forms application.

The following extensions are installed from the Marketplace:

enter image description here

What I tried:

1

Create console application, and reference using System.Windows.Forms;: However, this requires me to reference this namespace:

The type or namespace 'Forms' does not exist in the namespace "System.Windows"

So I tried to add this using the NuGet Package Manager: Add Package command, but here I can't find the package System.Windows.Forms.

One last thing I could think of was to manually add a reference to the .csproj file:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>netcoreapp2.0</TargetFramework>
    <RuntimeIdentifiers>win7-x64</RuntimeIdentifiers>
  </PropertyGroup>
  <ItemGroup>
    <PackageReference Include="Newtonsoft.Json" Version="10.0.3"/>
    <PackageReference Include="System.Windows.Forms" HintPath = "\..\WINDOWS\Microsoft.NET\Framework\v4.0.30319"/>
  </ItemGroup>
</Project>

But when running dotnet restore this gives warnings:

warning NU1604: Project dependency System.Windows.Forms does not contain an inclusive lower bound. Include a lower bound in the dependency version to ensure consistent restore results.
warning NU1701: Package 'System.Windows.Forms 4.0.0' was restored using '.NETFramework,Version=v4.6.1' instead of the project target framework '.NETCoreApp,Version=v2.0'. This package may not be fully compatible with your project.

And when run gives the following exception:

System.BadImageFormatException: Could not load file or assembly 'System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'. Reference assemblies should not be loaded for execution.  They can only be loaded in the Reflection-only loader context. (Exception from HRESULT: 0x80131058) ---> System.BadImageFormatException: Cannot load a reference assembly for execution.

2

Create a single .cs file, and compile that with csc. This way I can use Forms, but I lose the functionality of dotnet handling other dependencies.

Question

I feel like this issue suffers from the XY problem. I am new to C# and .NET, so I am not sure where I am failing, whether what I am trying to do is even possible.

So my question is, how do I create a windows forms application using the dotnet new command?

c#
.net
winforms
visual-studio-code
asked on Stack Overflow Oct 5, 2017 by JAD • edited Oct 5, 2017 by Lex Li

2 Answers

6

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.

answered on Stack Overflow Feb 9, 2019 by Pavel Sapehin
4

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:

.NET frameworks

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>
answered on Stack Overflow Oct 5, 2017 by JAD • edited Oct 6, 2017 by JAD

User contributions licensed under CC BY-SA 3.0