可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
I am getting a FileNotFoundException on a Windows Forms Application project, with the following message:
Could not load file or assembly 'System.Drawing, Version=4.0.0.0,
Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies.
The system cannot find the file specified.
To replicate the problem:
- Select New, Project, choose .Net Framework 2.0 as the target and pick Windows Forms Application as the project type.
- On the properties of the form created by default, select a value for the Icon property. Any .ico file will do. This will embed the file on the resx file.
- Compile and run the application.
When I do this, the program stops on the line this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
with the following exception:
System.IO.FileNotFoundException was unhandled
Message=Could not load file or assembly 'System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies. The system cannot find the file specified.
Source=mscorlib
FileName=System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a
I’m getting this on Visual Studio 2010 SP1, recently installed on Windows 8 Developer Preview. If I change the project properties to target .Net Framework 4, the error goes away.
On the Form1.resx file, I can see that the version of the System.Drawing assembly is explicitly stated as 2.0:
<assembly alias="System.Drawing" name="System.Drawing, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
Any ideas?
回答1:
I've found a possible solution, please try this:
Open the resx File in the Designer and set the accessmodifier from public to no code generation.
Edit: there's a workaround, but very annoying though.
- Open Form in Designer and make needed GUI changes. Close designer and save
- Compile project and receive RESX compile error (only forms with Imagelist should have this problem)
- Double-click resx compile error to open resx file.
- Scroll to top of imagestream.
- Edit the top line of the Image stream:
AAEAAAD/////AQAAAAAAAAAMAgAAAFdTeXN0ZW0uV2luZG93cy5Gb3JtcywgVmVyc2lvbj00LjAuMC4w
TO
AAEAAAD/////AQAAAAAAAAAMAgAAAFdTeXN0ZW0uV2luZG93cy5Gb3JtcywgVmVyc2lvbj0yLjAuMC4w
- Close and save resx file and recompile.
**NOTE: the only difference are the characters at end "j00LjAuMC4w' to "j0yLjAuMC4w"
This needs to be done EVERY TIME you open the form in Designer mode.
Microsoft says they are going to fix it in the next VS version...
Source: http://connect.microsoft.com/VisualStudio/feedback/details/532584/error-when-compiling-resx-file-seems-related-to-beta2-bug-5252020
回答2:
This is a bug. I have seen it too. It happens because your .resx file is pointing to 4.0.0.0 version of System.Drawing where one does not exist. To overcome this problem i usually edit the .resx in notepad to change 4.0.0.0 to 2.0.0.0. The bug is introduced by following the exact steps that you have outlined.
回答3:
This problem can occur if .net 4.5 preview resgen is used to create the resource files.
I have the same problem on my laptop(Windows 7, VS2010 Premium, VS11 Developer Preview).
I got this problem with a simple forms project when i say 'localizable=true' on a form. In my case are no image data involved. The project is set to .net 3.5
private void InitializeComponent()
{
System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(Form1));
this.SuspendLayout();
//
// Form1
//
resources.ApplyResources(this, "$this"); //exception Could not load file or assembly 'System.Drawing, Version=4.0.0.0,
If i then copy this project to another machine(Windows 7, VS2010 Premium) and try to debug it, the error remains.
The error goes away if i clean the solution(not the project)(or delete bin/obj by hand)
If i then copy this solution back to the my laptop, the error is gone, but i cant see the form again in design view 'Error message: at System.ComponentModel.Design.Serialization.CodeDomDesignerLoader.EnsureDocument(IDesignerSerializationManager manager)'
The reason for all this seems to be the .net version in the *.Designer.cs files.
- Runtime Version: 4.0.30319.239 on the computer where it works,
- Runtime Version: 4.0.30319.17020 on the laptop where i get the exception.
Can anyone tell me where i can configure which resgen version is used when dealing with .net 3.5 projects?
回答4:
I had this problem and found it oddly had to do with .Net 4.5 beta. Uninstalling that, and reinstalling .Net 4.0 caused the problem to go away. Not sure if there's a way to have .Net 4.5 beta installed and use resources from a .Net 2.0 project simultaneously.
回答5:
I had the same issue.
I repaired .net 4.5.1 and it fixed it.
回答6:
I recently had the same error message when a customer asked me to downgrade an application.
Using Visual Studio 2010 Professional I changed the target framework from .NET Framework 4 to .NET Framework 3.5. Building then failed with the specified error message. The solution was to remove the image file causing the problem from the application resources. Adding it again, the System.Drawing version was listed as 2.0.0.0, and building succeeded.
回答7:
I ran into the same issue and none of the above suggestions worked for me, so I did the following :
Open you Project
Go to the Solution Explorer
Expand the Reference group
Delete the System.Drawing reference
Right click on Reference group
Add Reference
On the ".NET" tab, search for System.Drawing to add the right reference
回答8:
I had same issue.. with my framework 2.0 Project keep referencing 4.0 dll. The way I was able to fix it... simply go to your project reference -> select "Syste.Drawing" -> Properties and there select use exact version=true
This will force to use 2.0 framework dll.
Hope this helps.
Cheers!!!
回答9:
I have also experienced this problem, and in my case the cause was building two essentially duplicate projects in parallel, one targeting .NET 2.0 and the other targeting .NET 4.0, both containing mostly the same code and resources. If the timing was right, the 2.0 project would pick up an output file from the 4.0 project, and end up referencing 4.0 libraries.
I fixed it by building the projects in series, by making one depend on the other.
(I appreciate that this was probably not the cause of the original problem as posted by @Leonardo, but it may be helpful for future visitors who encounter this error with a similar dual-project setup.)
回答10:
I met with same issue in Xamarin Project
Really this wont be worked in Xamarin since it is windows dll.
Take a look at discussion,
https://forums.xamarin.com/discussion/14340/adding-a-reference-to-net-assembly
回答11:
This isn't relevant for the OP's situation, but if you're getting this error in connection with using the ResourceWriter
class to convert a .resx
file into a .resources
file, then read on. I ran into this error and the only way I found to fix it was to scan the data for "System.Drawing, Version=4.0.0.0"
and replace it with "System.Drawing, Version=2.0.0.0"
. Here's my code (this is a part of a Roslyn modification):
/// <summary>
/// Method that gets called by ManagedResource.WriteData() in project CodeAnalysis during code
/// emitting to get the data for an embedded .resx file. Caller guarantees that the returned
/// MemoryStream object gets disposed.
/// </summary>
/// <param name="resourceFullFilename">full path and filename for .resx file to embed</param>
/// <param name="targetLessThan4">true if necessary to change System.Drawing from 4.0.0.0 to 2.0.0.0</param>
/// <returns>MemoryStream containing .resources file data for the .resx file</returns>
[SuppressMessage("Microsoft.Reliability", "CA2000:Dispose objects before losing scope")]
private static MemoryStream ProvideResourceDataForResx(string resourceFullFilename,
bool targetLessThan4)
{
MemoryStream shortLivedBackingStream = new MemoryStream();
using (ResourceWriter resourceWriter = new ResourceWriter(shortLivedBackingStream))
{
using (ResXResourceReader resourceReader = new ResXResourceReader(resourceFullFilename))
{
IDictionaryEnumerator dictionaryEnumerator = resourceReader.GetEnumerator();
while (dictionaryEnumerator.MoveNext())
{
string resourceKey = dictionaryEnumerator.Key as string;
if (resourceKey != null) // Should not be possible
resourceWriter.AddResource(resourceKey, dictionaryEnumerator.Value);
}
}
}
// Get reference to the buffer used by shortLivedBackingStream, which is now closed because
// resourceWriter was disposed. If relevant, fix version number for System.Drawing.
byte[] backingStreamBuffer = shortLivedBackingStream.GetBuffer();
if (targetLessThan4)
ChangeSystemDrawingVersionNumber(backingStreamBuffer);
// Create new MemoryStream because shortLivedBackingStream is closed
return new MemoryStream(backingStreamBuffer);
}
/// <summary>
/// Method to change the System.Drawing version number from "4.0.0.0" to "2.0.0.0" in the
/// binary data that represents a .resources file. This implementation is based on the
/// assumption that character data in the .resources file is in UTF-8 encoding.
/// </summary>
private static void ChangeSystemDrawingVersionNumber(byte[] dataBuffer)
{
byte[] byteArray1 = Encoding.UTF8.GetBytes("System.Drawing, Version=4.0.0.0");
byte[] byteArray2 = Encoding.UTF8.GetBytes("System.Drawing, Version=2.0.0.0");
for (int i = 0; i < dataBuffer.Length - byteArray1.Length; i++)
if (ArrayEquals(byteArray1, dataBuffer, i))
Array.Copy(byteArray2, 0, dataBuffer, i, byteArray2.Length);
}
/// <summary>
/// Method to test for a byte array in a larger byte array that is being searched. No error
/// checking is done - it's assumed an indexing error is not possible.
/// </summary>
private static bool ArrayEquals(byte[] searchArray, byte[] searchedArray,
int searchedArrayIndex)
{
for (int i = 0; i < searchArray.Length; i++)
if (searchArray[i] != searchedArray[searchedArrayIndex + i])
return false;
return true;
}
Edit: In a previous version of this answer I used a ResourceWriter.TypeNameConverter
method. That worked in some situations, but not in others, and there's a bug report that indicates there can be problems with that code, at least for the corresponding code in .Net Core: https://github.com/dotnet/corefx/issues/11083 .