“无法加载.Net 2.0,VS2010和Windows 8上的文件或程序集System.Drawing或其中一个依赖项”错误

我在Windows窗体应用程序项目上收到FileNotFoundException,并显示以下消息:

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. 

要复制问题:

  • 选择New,Project,选择.Net Framework 2.0作为目标,并选择Windows Forms Application作为项目类型。
  • 在默认情况下创建的表单的属性上,选择Icon属性的值。 任何.ico文件都可以。 这将把文件嵌入resx文件。
  • 编译并运行应用程序。

当我这样做时,程序就停止了这一行this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon"))); 除以下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 

我在最近安装在Windows 8开发人员预览版上的Visual Studio 2010 SP1上得到了这个。 如果我将项目属性更改为目标.Net Framework 4,则错误消失。

在Form1.resx文件中,我可以看到System.Drawing程序集的版本明确声明为2.0:

  

有任何想法吗?

我找到了一个可能的解决方案,请试试这个:

在Designer中打开resx文件,并将访问修改器从公共设置为无代码生成。

编辑:有一个解决方法,但非常烦人。

  1. 在Designer中打开表单并进行所需的GUI更改。 关闭设计师并保存
  2. 编译项目并收到RESX编译错误(只有带有Imagelist的表单应该有这个问题)
  3. 双击resx编译错误以打开resx文件。
  4. 滚动到图像流的顶部。
  5. 编辑图像流的顶行:AAEAAAD ///// AQAAAAAAAAAMAgAAAFdTeXN0ZW0uV2luZG93cy5Gb3JtcywgVmVyc2lvbj00LjAuMC4w到AAEAAAD ///// AQAAAAAAAAAMAgAAAFdTeXN0ZW0uV2luZG93cy5Gb3JtcywgVmVyc2lvbj0yLjAuMC4w
  6. 关闭并保存resx文件并重新编译。

**注意:唯一的区别是结尾处的字符“j00LjAuMC4w”到“j0yLjAuMC4w”每次在Designer模式下打开表单时都需要这样做。

微软称他们将在下一个VS版本中修复它……

来源: http : //connect.microsoft.com/VisualStudio/feedback/details/532584/error-when-compiling-resx-file-seems-related-to-beta2-bug-5252020

这是一个错误。 我也看过了。 这是因为您的.resx文件指向System.Drawing的4.0.0.0版本,其中一个不存在。 为了解决这个问题,我通常在记事本中编辑.resx以将4.0.0.0更改为2.0.0.0。 通过遵循您概述的确切步骤引入该错误。

如果.net 4.5预览resgen用于创建资源文件,则可能会出现此问题。

我的笔记本电脑上存在同样的问题(Windows 7,VS2010 Premium,VS11开发人员预览版)。 当我在表单上说’localizable = true’时,我遇到了一个简单的表单项目的问题。 在我的情况下,没有涉及图像数据。 该项目设置为.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, 

如果我然后将此项目复制到另一台计算机(Windows 7,VS2010 Premium)并尝试调试它,则错误仍然存​​在。 如果我清理解决方案(而不是项目)(或删除bin / obj手动)错误消失了如果我然后将此解决方案复制回我的笔记本电脑,错误消失了,但我无法再在设计视图中看到该表单'Error message: at System.ComponentModel.Design.Serialization.CodeDomDesignerLoader.EnsureDocument(IDesignerSerializationManager manager)'

所有这些的原因似乎是* .Designer.cs文件中的.net版本。

  1. 运行时版本:4.0.30319.239在它运行的计算机上,
  2. 运行时版本:4.0.30319.17020在我得到exception的笔记本电脑上。

任何人都可以告诉我在处理.net 3.5项目时我可以配置哪个resgen版本?

我遇到了这个问题,发现奇怪的是与.Net 4.5 beta有关。 卸载它,并重新安装.Net 4.0导致问题消失。 不确定是否有安装.Net 4.5 beta的方法并同时使用.Net 2.0项目中的资源。

我遇到过同样的问题。

我修复了.net 4.5.1并修复了它。

当客户要求我降级应用程序时,我最近收到了相同的错误消息。

使用Visual Studio 2010 Professional我将目标框架从.NET Framework 4更改为.NET Framework 3.5。 然后使用指定的错误消息构建失败。 解决方案是从应用程序资源中删除导致问题的图像文件。 再次添加它,System.Drawing版本被列为2.0.0.0,并且构建成功。

我遇到了同样的问题,上述建议都没有对我有用,所以我做了以下几点:

打开你的项目
转到解决方案资源管理器
展开“引用”组
删除System.Drawing引用
右键单击“引用”组
添加参考
在“.NET”选项卡上,搜索System.Drawing以添加正确的引用

我有同样的问题..我的框架2.0项目继续引用4.0 dll。 我能够修复它的方式……只需转到你的项目参考 – >选择“Syste.Drawing” – >属性,然后选择使用exact version = true

这将强制使用2.0框架DLL。

希望这可以帮助。

干杯!!!

我也经历过这个问题,在我的情况下,原因是并行构建了两个基本上重复的项目,一个针对.NET 2.0,另一个针对.NET 4.0,两者都包含大部分相同的代码和资源。 如果时机正确,2.0项目将从4.0项目中获取输出文件,最终引用4.0库。

我通过串联构建项目来修复它,使一个项目依赖于另一个项目。

(我很欣赏这可能不是由@Leonardo发布的原始问题的原因,但它可能对将来遇到此类错误的访问者提供类似的双项目设置有帮助。)

我在Xamarin项目中遇到了同样的问题

真的,这不会在Xamarin中工作,因为它是windows dll。

看看讨论,

https://forums.xamarin.com/discussion/14340/adding-a-reference-to-net-assembly

这与OP的情况无关,但如果您在使用ResourceWriter类将.resx文件转换为.resources文件时遇到此错误,请继续阅读。 我遇到了这个错误,我发现修复它的唯一方法是扫描数据"System.Drawing, Version=4.0.0.0"并将其替换为"System.Drawing, Version=2.0.0.0" 。 这是我的代码(这是Roslyn修改的一部分):

  ///  /// 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. ///  /// full path and filename for .resx file to embed /// true if necessary to change System.Drawing from 4.0.0.0 to 2.0.0.0 /// MemoryStream containing .resources file data for the .resx file [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); } ///  /// 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. ///  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); } ///  /// 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. ///  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; } 

编辑:在此答案的先前版本中,我使用了ResourceWriter.TypeNameConverter方法。 这在某些情况下有效,但在其他情况下则没有,并且有一个错误报告表明该代码可能存在问题,至少对于.Net Core中的相应代码: https : //github.com/dotnet/corefx/issues / 11083 。