Home > Net >  .net error - are you missing an assembly reference?
.net error - are you missing an assembly reference?

Time:06-17

I've created .net program using dotnet new console -o MyApp -f net6.0 command just as documented in https://dotnet.microsoft.com/en-us/learn/dotnet/hello-world-tutorial/create

I've modified the file:

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);

But when I run PS C:\Users\box\Desktop\dotnet\MyApp> dotnet run

I get error - how to fix it?

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.

I write program in Notepad.exe - do I need special program to run it? I am using in my program https://docs.microsoft.com/en-us/dotnet/api/system.windows.forms.systeminformation?view=windowsdesktop-6.0 and it mentions:

"Namespace: System.Windows.Forms"

"Assembly: System.Windows.Forms.dll"

Somehow my program is missing reference to this I suppose but how to do it?

CodePudding user response:

Every dependency and namespaces you want to use in your code must be either in your .net SDK or be installed (added the reference of its NuGet to your project)

You can use many ways to install the following package that is not built in .net 6.0 (this package is built-in .Net Framework and its deprecated).

  1. go to the project directory and use the command: dotnet add package System.Windows.Forms
  2. add this line as a reference inside of ItemGroup tag inside of your MyApp.csproj file: <PackageReference Include="System.Windows.Forms" Version="4.0.0" />
  3. you can use Nuget galleries extensions in different IDEs
  4. you can check out the Nuget package page, for example, System.Windows.Forms
  • Related