CodePudding user response:
Which TargetFramework do you use? If you use some classic framework like net48
everything should work fine.
For the modern target frameworks (like net6.0
) it is impossible to "just reference"
System.Windows.Forms
. For example if you add a plain reference to the project file like
<ItemGroup>
<Reference Include="System.Windows.Forms" />
</ItemGroup>
The project won't build properly. MSBuild produces the next warning:
Microsoft.Common.CurrentVersion.targets(2301, 5):
[MSB3245] Could not resolve this reference. Could not locate the assembly "System.Windows.Forms".
Check to make sure the assembly exists on disk. If this reference is required by your code, you may get compilation errors.
If you want to work with windows forms in modern console apps, you have to add these lines to the project file instead of adding a single reference:
<PropertyGroup>
<TargetFramework>net6.0-windows</TargetFramework>
<UseWindowsForms>true</UseWindowsForms>
</PropertyGroup>