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:
What I tried:
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.
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.
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?
It is a little bit tough, time-consuming, and complex to use WPF as compared to WinForms.
The single most important difference between WinForms and WPF is the fact that while WinForms is simply a layer on top of the standard Windows controls (e.g. a TextBox), WPF is built from scratch and doesn't rely on standard Windows controls in almost all situations.
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
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With