C# .Net 2.0、VS2010 和 Windows 8 上的“无法加载文件或程序集 System.Drawing 或其依赖项之一”错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9190885/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
"Could not load file or assembly System.Drawing or one of its dependencies" error on .Net 2.0, VS2010 and Windows 8
提问by Leonardo
I am getting a FileNotFoundException on a Windows Forms Application project, with the following message:
我在 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.
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.
- 选择 New、Project,选择 .Net Framework 2.0 作为目标,然后选择 Windows Forms Application 作为项目类型。
- 在默认创建的表单的属性上,为 Icon 属性选择一个值。任何 .ico 文件都可以。这会将文件嵌入到 resx 文件中。
- 编译并运行应用程序。
When I do this, the program stops on the line this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));with the following exception:
当我这样做时,程序停止在行上this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));,但有以下异常:
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.
我在最近安装在 Windows 8 Developer Preview 上的 Visual Studio 2010 SP1 上得到了这个。如果我将项目属性更改为面向 .Net Framework 4,错误就会消失。
On the Form1.resx file, I can see that the version of the System.Drawing assembly is explicitly stated as 2.0:
在 Form1.resx 文件中,我可以看到 System.Drawing 程序集的版本明确声明为 2.0:
<assembly alias="System.Drawing" name="System.Drawing, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
Any ideas?
有任何想法吗?
采纳答案by vulkanino
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.
在设计器中打开 resx 文件并将 accessmodifier 从 public 设置为 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.
- 在 Designer 中打开 Form 并进行所需的 GUI 更改。关闭设计器并保存
- 编译项目并收到RESX编译错误(只有带有Imagelist的表单才会有这个问题)
- 双击 resx 编译错误打开 resx 文件。
- 滚动到图像流的顶部。
- 编辑图像流的第一行: AAEAAAD////AQAAAAAAAAAMAgAAAFdTeXN0ZW0uV2luZG93cy5Gb3JtcywgVmVyc2lvbj00LjAuMC4w TO AAEAAAD////AQAAAAAAAAAMAgAAAFdTeXN0ZW0uV2lvcybycgbv4lvcylvgvcgbc
- 关闭并保存 resx 文件并重新编译。
**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.
**注意:唯一的区别是“j00LjAuMC4w”到“j0yLjAuMC4w”结尾的字符。每次在设计器模式下打开表单时都需要执行此操作。
Microsoft says they are going to fix it in the next VS version...
微软表示他们将在下一个 VS 版本中修复它......
来源:http: //connect.microsoft.com/VisualStudio/feedback/details/532584/error-when-compiling-resx-file-seems-related-to-beta2-bug-5252020
回答by Sudarshan chandan
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.
这是一个错误。我也见过。发生这种情况是因为您的 .resx 文件指向 System.Drawing 的 4.0.0.0 版本,其中一个不存在。为了克服这个问题,我通常在记事本中编辑 .resx 以将 4.0.0.0 更改为 2.0.0.0。该错误是通过按照您概述的确切步骤引入的。
回答by Manuel
This problem can occur if .net 4.5 preview resgen is used to create the resource files.
如果使用 .net 4.5 预览 resgen 来创建资源文件,则会出现此问题。
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
我的笔记本电脑(Windows 7、VS2010 Premium、VS11 Developer Preview)也有同样的问题。当我在表单上说“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,
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)'
如果我然后将此项目复制到另一台机器(Windows 7,VS2010 Premium)并尝试调试它,错误仍然存在。如果我清理解决方案(不是项目)(或手动删除 bin/obj),错误就会消失如果我然后将此解决方案复制回我的笔记本电脑,错误就会消失,但我无法在设计视图中再次看到该表单'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.
这一切的原因似乎是 *.Designer.cs 文件中的 .net 版本。
- Runtime Version: 4.0.30319.239on the computer where it works,
- Runtime Version: 4.0.30319.17020on the laptop where i get the exception.
- 运行时版本:4.0.30319.239在它工作的计算机上,
- 运行时版本:4.0.30319.17020在我收到异常的笔记本电脑上。
Can anyone tell me where i can configure which resgen version is used when dealing with .net 3.5 projects?
谁能告诉我在哪里可以配置在处理 .net 3.5 项目时使用哪个 resgen 版本?
回答by Dax Fohl
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 anduse resources from a .Net 2.0 project simultaneously.
我遇到了这个问题,并发现它奇怪地与 .Net 4.5 beta 有关。卸载它并重新安装 .Net 4.0 导致问题消失。不确定是否有办法安装 .Net 4.5 beta并同时使用 .Net 2.0 项目中的资源。
回答by Journeyman
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.
使用 Visual Studio 2010 Professional,我将目标框架从 .NET Framework 4 更改为 .NET Framework 3.5。构建失败并显示指定的错误消息。解决方案是从应用程序资源中删除导致问题的图像文件。再次添加,System.Drawing版本被列为2.0.0.0,构建成功。
回答by NG.
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
打开您的项目
转到解决方案资源管理器
展开参考组
删除 System.Drawing 参考
右键单击参考组
添加参考
在“.NET”选项卡上,搜索 System.Drawing 以添加正确的参考
回答by Jon
I had the same issue.
我遇到过同样的问题。
I repaired .net 4.5.1 and it fixed it.
我修复了 .net 4.5.1 并修复了它。
回答by user4784388
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
我有同样的问题..我的框架 2.0 项目继续引用 4.0 dll。我能够修复它的方式......只需转到您的项目参考->选择“Syste.Drawing”->属性,然后选择使用精确版本=真
This will force to use 2.0 framework dll.
这将强制使用 2.0 框架 dll。
Hope this helps.
希望这可以帮助。
Cheers!!!
干杯!!!
回答by RichieHindle
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.
我也遇到过这个问题,就我而言,原因是并行构建两个本质上是重复的项目,一个针对 .NET 2.0,另一个针对 .NET 4.0,两者都包含大部分相同的代码和资源。如果时机合适,2.0 项目将从 4.0 项目中选取一个输出文件,并最终引用 4.0 库。
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.)
(我很欣赏这可能不是@Leonardo 发布的原始问题的原因,但它可能对未来使用类似双项目设置遇到此错误的访问者有所帮助。)
回答by Gobi M
I met with same issue in Xamarin Project
我在Xamarin 项目中遇到了同样的问题
Really this wont be worked in Xamarin since it is windows dll.
真的这不会在 Xamarin 中工作,因为它是 Windows dll。
Take a look at discussion,
看看讨论,
https://forums.xamarin.com/discussion/14340/adding-a-reference-to-net-assembly
https://forums.xamarin.com/discussion/14340/adding-a-reference-to-net-assembly

