Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I install System.Drawing.Common in a project that uses '.NETFramework,Version=v4.5.2'?

I am trying to write some unit tests in C# in a '.NETFramework,Version=v4.5.2' application but all tests give the next error:

'System.IO.FileNotFoundException : Could not load file or assembly 'System.Drawing.Common, Version=0.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51'. The system cannot find the file specified.'

When I try to install System.Drawing.Common I get the next error from the NuGet package:

Could not install package 'System.Drawing.Common 4.5.1'. You are trying to install this package into a project that targets '.NETFramework,Version=v4.5.2', but the package does not contain any assembly references or content files that are compatible with that framework. For more information, contact the package author.

I cannot change the application version or the framework (.NET Core 2.1) and any other trick I found online did not work (or generated more errors).

Help?

like image 615
Irina Gutanu Avatar asked Mar 04 '19 13:03

Irina Gutanu


3 Answers

in NuGet put this line : Install-Package System.Drawing.Common -Version 4.5.2

in .NET CLI put : dotnet add package System.Drawing.Common --version 4.5.2

in Paket CLI put : paket add System.Drawing.Common --version 4.5.2

like image 99
ayman lys Avatar answered Oct 22 '22 11:10

ayman lys


Had the same problem. I have cloned solution https://github.com/barnhill/barcodelib . It has two projects: library project targets .Net Standard 2.0 and refers to System.Drawings.Common. Example project depends on library and has reference to System.Drawings.Common. Example project was not compiling due to same error.

My solution was just to remove reference to System.Drawings.Common in nuget packages and readd it (rclick on Example project > Manage nuget packages > Browse Installed, remove the System.Drawings.Common package, and then add it back), unload project and then reload it again

Helped for me.

like image 26
Ivan P. Avatar answered Oct 22 '22 10:10

Ivan P.


I managed to solve it by restarting Visual Studio, changing the framework to 4.6.1 (which I could not do before) and adding the reference.

OP's solution migrated from the question to an answer.

like image 22
TylerH Avatar answered Oct 22 '22 12:10

TylerH