According to other answers, I try to add project reference. I open the reference manager to find System.Windows.Forms and add it, but the project reference shows "No items found". So how can I add the reference? enter image description here
2 Answers
As Jimi's comment suggests, you cannot directly reference System.Windows.Forms.dll because it's part of the runtime. Instead, add the following to your project file: <UseWindowsForms>true</UseWindowsForms> and make sure the target framework is set as <TargetFramework>net6.0-windows</TargetFramework> (and not only net6.0), because Windows Forms is only supported on Windows.
Comments
You need change <TargetFramework>net8.0</TargetFramework> to <TargetFramework>net8.0-windows</TargetFramework>, and add <UseWindowsForms>true</UseWindowsForms>
Update:
Following this proposal, I've update my .csproj file from project directory.
New lines in xml element are:
<PropertyGroup> <TargetFramework>net9.0-windows</TargetFramework> <UseWindowsForms>true</UseWindowsForms> </PropertyGroup> That helped me .
Edit Project File, then add<UseWindowsForms>true</UseWindowsForms>and change, if different,<TargetFramework>net6.0-windows</TargetFramework>. Rebuild.