添加链接
link之家
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接

Hello:
I know .NET 6.0 is released, but I don’t know how to create a WinForm App targeting .NET 6.0 with Visual Studio 2019 (Version 16.11.5) on Windows 10 (Version 21H1)
I can download the following packages:
.NET 6.0 SDK (v6.0.100) - Windows x64 Installer!
.NET 6.0 Desktop Runtime (v6.0.0) - Windows x64 Installer!
But when I create a WinForm App project, there is no place to target .NET 6.0.
Please give detailed instructions.
I know I can use Visual Studio 2022 can target .NET 6.0, but currently we can’t use Visual Studio 2022 yet, have to wait at least one year.
Thanks,

First off, for proof the following works, the following Visual Studio Solution was created in VS2019, runs fine. As a side note I have samples for DateOnly and TimeOnly struct and a few other goodies.

Under Options, Environment, Preview Features set Use previews of the .NET SDK to true , restart Visual Studio.

Restart, in a Visual Studio solution create a new Windows Form app.

To validate double click the project file in Solution Explorer and you should see

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <OutputType>WinExe</OutputType>
    <TargetFramework>net6.0-windows</TargetFramework>
    <Nullable>enable</Nullable>
    <UseWindowsForms>true</UseWindowsForms>
  </PropertyGroup>
</Project>

Or create via PowerShell at the root folder of a Visual Studio solution

dotnet new winforms --name WinApp1 --output WinApp1
dotnet sln add .\WinApp1\WinApp1.csproj
												

Hello:
I tried to follow your advice from VS 2019, see the pictures.
But I don't know if the settings are OK or not.
I have concerns that the project is targetting .NET 6.0 or not, how to know this?

This is not bad advice... The context of preview here is not the framework, but a feature of Visual Studio. You will be compiling against .Net 6.0 SDK full release, not a preview release.

By selecting the Visual Studio option, and installed .Net 6.0 SDK, you can use .Net 6.0 in Visual Studio 2019.

Looks like they finally transformed Error NETSDK1045 message from warning to an critical error. That workaround was working for sure at least in May 2022. Full error message is

C:\Program Files\dotnet\sdk\5.0.411\Sdks\Microsoft.NET.Sdk\targets\Microsoft.NET.TargetFrameworkInference.targets(141,5): error NETSDK1045: The current .NET SDK does not support targeting .NET 6.0. Either target .NET 5.0 or lower, or use a version of the .NET SDK that supports .NET 6.0.

So I suppose they applied that change directly in .NET 5.0 SDK. If you wish you may try SDK 5.0.408 from May 10, 2022 which is included in Visual Studio 16.11.14 - the workaround should still work in it. If not - try Visual Studio 16.11.13.

@Alexey Leonovich FWIW I happen to have VS2019 16.11.12 installed and I have the same hard error (not warning) about NETSDK1045. And dotnet --list-sdks reports that I have

   5.0.407 [C:\Program Files\dotnet\sdk]  
   6.0.301 [C:\Program Files\dotnet\sdk]  
   6.0.400 [C:\Program Files\dotnet\sdk]  
												

I've found my old test PC with Visual Studio 2019 Pro 16.11.5 and now it also produces hard error (not warning). This PC has .NET 5.0 SDK 5.0.402 and recently I've updated .NET 6 SDK to version 6.0.300 (runtime version is 6.0.5). So maybe described workaround depends on .NET 6 SDK version - you may try some very first versions. Or may be installing one of the latest .NET 6 SDK intself transforms warning to a hard error - cannot test it because I don't have clean PC nearby :(

@DaveInCaz I’ve just created a new VM with Windows 10 21H2 Pro x64, installed Visual Studio 2019 16.11.6 Pro (November 9, 2021) and .NET SDK 6.0.100 REL (November 8, 2021). Then I’ve created new Windows Forms App targeting .NET 5.0 and builded it. After that changed <TargetFramework> tag in .csproj file from net5.0-windows to net6.0-windows (also added <LangVersion> preview </LangVersion> line to be able to use C# 9.0 features) and Rebuilded my solution. NETSDK1182 is a warning (not hard error) and allows me to compile, run and debug my application.
I will try to figure out if the NETSDK1182 warning is converted to a hard error by a Visual Studio 2019 version update or a .NET 6 SDK version update…

This method did not work with the non-preview release version of dotnet-sdk-6.0.310-win-x64.exe with VS 2019.

Even with the Preview options selected in Visual Studio, while creating a new Project type .NET 6.0 was unavailable as a Target Framework.

Visual Studio 2019 is officially unsupported for .NET 6.0 development but there is a workaround that is still working for today (October 2022):

  • Install latest Visual Studio 2019 (I've tested on 16.11.20, 11 October 2022).
  • Intall latest .NET SDK from 6.0.1xx SDK branch (I've tested on 6.0.110, for 6.0.10 runtime, 11 October 2022). It is vital to use this SDK branch because NETSDK1182 warning was transformed into a hard error in 6.0.3xx SDK branch. 6.0.2xx SDK branch is not getting updates anymore.
  • Create new application of type you need targeting .NET 5.0. Compile it.
  • Open .csproj file and change <TargetFramework>net50</TargetFramework> to <TargetFramework>net60</TargetFramework> (in some cases it can be net5.0-windows - so change it to net6.0-windows). Optionally add <LangVersion>preview</LangVersion> to be able to use C#9.0 syntax.
  • Now you can compile, run and debug (don't pay attention to NETSDK1182 warning) some types of .NET 6.0 applications: class library, console, WinForms and WPF. ASP .NET Core applications doesn't compile for some reason, mybe there is some workaround but I don't have info about it.
  • Detailed information about .NET SDK version:
    So finally looks like I've managed to find it out:
    NETSDK1182 warning was transformed into a hard error in .NET SDK 6.0.300 (for .NET 6.0.5 runtime, Visual Studio 17.2). So don't use 6.0.3xx SDK branch (looks like same is correct for 6.0.4xx, etc).
    6.0.2xx SDK branch is out of support (since Visual Studio 17.1 is out of support), but 6.0.1xx SDK branch is still supported (since Visual Studio 17.0 is supported till 11 April 2023).
    Latest one is .NET SDK 6.0.110 (for latest .NET 6.0.10 runtime) and with it Visual Studio 2019 can compile, run and debug some types of .NET 6.0 applications: class library, console, WinForms and WPF. ASP .NET Core applications doesn't compile but not because of NETSDK1182. Tested with latest Visual Studio 2019 16.11.20.
    Good luck.

    From the doc, it is not supported : Download .NET 6.0 :

    Visual Studio support
    Visual Studio 2022 (v17.0)
    Visual Studio 2019 for Mac (v8.10)

    If I set net6.0-windows in <TargetFramework> in .csproj, it does not work on my 2019 version :
    I get
    Error NETSDK1045 The current .NET SDK does not support targeting .NET 6.0. Either target .NET 5.0 or lower, or use a version of the .NET SDK that supports .NET 6.0.

    and in the doc for .NET 6.0 SDK :
    This release is only compatible with Visual Studio 2022 (v17.0)

    They are really different code bases.

    As visual studio 2022 for the Mac is still in preview, so 2019 was updated for .net 6.

    For vs 2022, large portions of the code was converted to .net. Much of this code will be shared between vs 2022 windows and Mac. An important piece of code was the new debugger and hot load.

    Most likely vs2019 will not support the new debugger.

    @Castorix31 is correct. .NET 6 only supports following Visual Studio versions,

  • Visual Studio 2022 (v17.0)
  • Visual Studio 2019 for Mac (v8.10)
  • More Information:
    Announcing .NET 6 — The Fastest .NET Yet
    https://dotnet.microsoft.com/download/dotnet/6.0