.net错误-是否缺少程序集引用?

klsxnrf1  于 2023-06-07  发布在  .NET
关注(0)|答案(2)|浏览(123)

我用dotnet new console -o MyApp -f net6.0命令创建了.net程序,就像在https://dotnet.microsoft.com/en-us/learn/dotnet/hello-world-tutorial/create中记录的那样
我修改了文件:
Program.cs

global using System.Windows.Forms;

System.Console.WriteLine("Primary screen resolution: " + SystemInformation.PrimaryMonitorSize);
System.Console.WriteLine("Virtual size: " + SystemInformation.VirtualScreen);
System.Console.WriteLine("Monitors count: " + SystemInformation.MonitorCount);

但是当我运行PS C:\Users\box\Desktop\dotnet\MyApp> dotnet run
我得到错误-如何修复它?

C:\Users\box\Desktop\dotnet\MyApp\Program.cs(1,29): error CS0234: The type or namespace name 'Form
s' does not exist in the namespace 'System.Windows' (are you missing an assembly reference?) [C:\U
sers\box\Desktop\dotnet\MyApp\MyApp.csproj]

The build failed. Fix the build errors and run again.

我在Notepad.exe中编写程序-我需要特殊的程序来运行它吗?我在我的程序中使用https://learn.microsoft.com/en-us/dotnet/api/system.windows.forms.systeminformation?view=windowsdesktop-6.0,它提到:
“命名空间:System.Windows.Forms”
“程序集:System.Windows.Forms.dll”
不知何故,我的程序是失踪的引用,我想,但如何做到这一点呢?

kqlmhetl

kqlmhetl1#

你想在你的代码中使用的每个依赖和命名空间必须在你的.net SDK中或者已经安装(在你的项目中添加了它的NuGet的引用)
您可以使用许多方法来安装以下未内置于.net 6.0中的软件包(此软件包内置于.Net Framework,其已弃用)。
1.转到项目目录并使用以下命令:dotnet add package System.Windows.Forms
1.在MyApp.csproj文件中的ItemGroup标记中添加这一行作为引用:<PackageReference Include="System.Windows.Forms" Version="4.0.0" />
1.您可以在不同的IDE中使用Nuget gallery扩展
1.您可以查看Nuget软件包页面,例如System.Windows.Forms

kq0g1dla

kq0g1dla2#

System.Windows.Forms命名空间不会自动导入,需要添加NuGet包或者新建一个带有dotnet new winforms的Forms项目,

相关问题