8

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?

6
  • Maybe this can help: stackoverflow.com/documentation/winforms/1018/… (note, this is on SO Documentation which may disappear sooner or later) Commented Oct 5, 2017 at 10:06
  • @PeterB Documentation is already discontinued and the post has nothing to do with the question Commented Oct 5, 2017 at 10:23
  • @PeterB That is what I used for attempt 2. However, this breaks the importing of the Newtonsoft.Json package. Which would be a different question altogether. Since that wouldn't directly be a solution, I am interested to know whether it is possible to do with dotnet. Commented Oct 5, 2017 at 10:23
  • Show what Visual Studio features you have installed, by checking Visual Studio 2017 installer please. Otherwise, this question cannot be answered. Commented Oct 5, 2017 at 14:21
  • @LexLi This is about Visual Studio Code, not VS 2017. It is not installed through the installed with feature selection as you mentioned. Commented Oct 5, 2017 at 14:31

2 Answers 2

8

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.

Sign up to request clarification or add additional context in comments.

Comments

5

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>

4 Comments

You should go back to use Visual Studio on Windows. Doing WinForms/WPF development in VSCode is not realistic an approach. Before Microsoft officially supports WinForms/WPF in this new project system, it might work but also might break. For example, System.Windows.Forms might be a Reference instead of PackageReference.
@LexLi Yeah, I am aware that this is a hacky workaround. Due to licensing issues I am unable to use VS 2017, so at the moment this is the only way for me to do it. I'll add to the answer that this is not the good way to do it.
if licensing is the issue to use Visual Studio, you should find that SharpDevelop is usually enough for small projects.
@LexLi Thanks, I'll check that out

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.