.NET 6 Console 引用 System.Windows.Forms

1,056 阅读2分钟

.NET 6 Console 引入 System.Windows.Forms

.csproj 修改前

<Project Sdk="Microsoft.NET.Sdk">
	<PropertyGroup>
		<OutputType>Exe</OutputType>
		<TargetFramework>net6.0</TargetFramework>
		<ImplicitUsings>enable</ImplicitUsings>
		<Nullable>enable</Nullable>
                <Platforms>AnyCPU;x86</Platforms>
	</PropertyGroup>	
</Project>

.csproj 修改后

<Project Sdk="Microsoft.NET.Sdk">
	<PropertyGroup>
		<OutputType>Exe</OutputType>
		<TargetFramework>net6.0-windows</TargetFramework>
		<ImplicitUsings>enable</ImplicitUsings>
		<Nullable>enable</Nullable>
		<Platforms>AnyCPU;x86</Platforms>
		<UseWindowsForms>true</UseWindowsForms>	
	</PropertyGroup>	
</Project>

重点项

<TargetFramework>net6.0-windows</TargetFramework>     #指定windows平台
<UseWindowsForms>true</UseWindowsForms>	              #开启windows form 支持

参考

  1. .NET Core 3.1 参考

(´∇`) 被你发现啦~ 在.Net Core中引入System.Windows.Forms – 凤曦的小窝 (rayfalling.com)

  1. .NET 5 + 参考

将 Windows 窗体应用迁移到 .NET 5 - Windows 窗体 .NET |微软学习 (microsoft.com)

异常解决

.NET5+ 出现加载 System.Windows.Forms 6.0.2.0 异常时

异常解决:

Unable to load one or more of the requested types. Could not load file or assembly 'System.Windows.Forms, Version=6.0.2.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'. 系统找不到指定的文件

解决方法:

  1. 查看本机.net runtime
dotnet --list-runtimes

结果:

Microsoft.AspNetCore.App 6.0.16 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App] Microsoft.AspNetCore.App 7.0.5 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App] Microsoft.NETCore.App 3.1.28 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App] Microsoft.NETCore.App 6.0.16 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App] Microsoft.NETCore.App 7.0.5 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App] Microsoft.WindowsDesktop.App 6.0.16 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App] Microsoft.WindowsDesktop.App 7.0.5 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]

  1. 配置 .csproj

指定windowsForms指定版本对应本机已存在

<ItemGroup Condition="'$(TargetFramework)' == 'net6.0-windows'">
		<FrameworkReference Update="Microsoft.WindowsDesktop.App;Microsoft.WindowsDesktop.App.WPF;Microsoft.WindowsDesktop.App.WindowsForms" TargetingPackVersion="6.0.16" />
</ItemGroup>

参考:

Did the System.Windows.Forms assembly version intentionally change in 6.0.2? · Issue #6693 · dotnet/winforms (github.com)

Issue building with Windows Desktop 6.0.2 · Issue #7176 · dotnet/core (github.com)

  1. 拷贝缺少的 System.Windows.Forms.dll 与 .exe或.dll 同级目录

这里的拷贝路径为:

C:\Program Files (x86)\dotnet\shared\Microsoft.WindowsDesktop.App\6.0.16

拷贝 System.Windows.Forms.dll 至 .exe或.dll 同级目录

4、5看后续依据实际情况拷贝

  1. 运行后发现缺少 System.Windows.Forms.Primitives.dll

Unable to load one or more of the requested types. Could not load file or assembly 'System.Windows.Forms.Primitives, Version=6.0.2.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'. 系统找不到指定的文件。

同样从3中的路径拷贝 System.Windows.Forms.Primitives.dll 至 .exe或.dll 同级目录

  1. 运行后发现缺少 System.Drawing.Common.dll

Unable to load one or more of the requested types. Could not load file or assembly 'System.Drawing.Common, Version=6.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51'. 系统找不到指定的文件。

同样从3中的路径拷贝 System.Drawing.Common.dll 至 .exe或.dll 同级目录

  1. 运行后发现 缺少 Accessibility.dll

System.IO.FileNotFoundException: Could not load file or assembly 'Accessibility, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'. The system cannot find the file specified.

同样从3中的路径拷贝 Accessibility.dll 至 .exe或.dll 同级目录

参考:Windows – Could not load file or assembly Accessibility, Version=4.0.0.0 – Lab Core | The Lab of MrNetTek (eddiejackson.net)