C# 无法加载文件或程序集...尝试加载格式不正确的程序 (System.BadImageFormatException)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11370344/
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 ... An attempt was made to load a program with an incorrect format (System.BadImageFormatException)
提问by BlueRaja - Danny Pflughoeft
I have two projects, ProjectAand ProjectB. ProjectBis a console application, which depends on ProjectA. Yesterday, everything was working fine, but suddenly today when I run ProjectBI get this:
我有两个项目,ProjectA和ProjectB. ProjectB是一个控制台应用程序,它依赖于ProjectA. 昨天,一切正常,但今天我跑步时突然发现ProjectB:
BadImageFormatException was unhandled:
Could not load file or assembly 'ProjectA, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' or one of its dependencies. An attempt was made to load a program with an incorrect format.
BadImageFormatException 未处理:
无法加载文件或程序集“ProjectA,版本=1.0.0.0,Culture=neutral,PublicKeyToken=null”或其依赖项之一。试图加载格式不正确的程序。
Both are just regular projects, with no dependencies on any other non-.Net projects. Both are fully .Net - there is no native code, and no P/Invoke. I have other projects which depend on ProjectAand still work just fine.
两者都只是常规项目,不依赖于任何其他非 .Net 项目。两者都是完全 .Net - 没有本机代码,也没有 P/Invoke。我还有其他项目依赖ProjectA并且仍然可以正常工作。
Things I have tried:
我尝试过的事情:
- Make sure both projects are set to "Any CPU," with the buildcheckbox checked. They are.
- Make sure both projects are for the same Target Framework (.Net 4.0 Client Profile).
- Under ProjectB --> References --> ProjectA --> Properties, make sure "Copy Local"is set to "True"_ (I verified that ProjectA.dll is being copied correctly)
- Clean/Rebuild the solution. I even tried manually deleting the /bin and /obj folders in both projects.
- Restart Visual Studio. Restart my computer.
- Check out an entirely new copy of the repository.
- 确保两个项目都设置为“任何 CPU”,并选中构建复选框。他们是。
- 确保两个项目都用于相同的目标框架(.Net 4.0 Client Profile)。
- 在 ProjectB --> References --> ProjectA --> Properties 下,确保“Copy Local”设置为“True”_ (我验证了 ProjectA.dll 被正确复制)
- 清理/重建解决方案。我什至尝试手动删除两个项目中的 /bin 和 /obj 文件夹。
- 重新启动 Visual Studio。重新启动我的电脑。
- 查看存储库的全新副本。
But I still get the same error. I have no idea what I did to cause this, nor how to fix it. Any ideas?
但我仍然得到同样的错误。我不知道我做了什么导致这个,也不知道如何解决它。有任何想法吗?
采纳答案by Icemanind
I am pretty sure you're having a 32-bit / 64-bit conflict. It sounds like your main project might be set to 32-bit while the class its referencing is set to 64-bit. Try looking at this SO questionand this one too. Between the two of them, you should be able to figure out your problem.
我很确定您遇到了 32 位 / 64 位冲突。听起来您的主项目可能设置为 32 位,而其引用的类设置为 64 位。尝试查看这个 SO question和this one。在他们两个之间,您应该能够弄清楚您的问题。
回答by Icemanind
I had this same problem. I had set Project A's "Platform Target" ("Project A"(Right Click)->Properties->Build->"Platform Target") to x86 but kept Project B's at "Any CPU". Setting Project B's to "x86" fixed this.
我有同样的问题。我已将项目 A 的“平台目标”(“项目 A”(右键单击)-> 属性-> 构建->“平台目标”)设置为 x86,但将项目 B 保持在“任何 CPU”。将项目 B 设置为“x86”修复了这个问题。
回答by Flood Techs
I had the same issue with multiple projects in the same solution, i ended up setting all of the target frameworks to .NET Framework 4 and x86 for the target CPU and it finally successfully compiled.
我在同一个解决方案中的多个项目中遇到了同样的问题,我最终将所有目标框架设置为目标 CPU 的 .NET Framework 4 和 x86,并最终成功编译。
回答by Jeremy Ray Brown
I encountered the same issue. It popped up out of the blue and that seemed strange to me.
我遇到了同样的问题。它突然出现了,这对我来说似乎很奇怪。
In the Exception snapshot, for the FusionLog, I saw the following within its message:
在异常快照中,对于 FusionLog,我在其消息中看到以下内容:
... C:\Windows\Microsoft.NET\Framework64 ...
... C:\Windows\Microsoft.NET\Framework64 ...
More about the fusion log: http://msdn.microsoft.com/en-us/library/e74a18c4(v=vs.110).aspx
有关融合日志的更多信息:http: //msdn.microsoft.com/en-us/library/e74a18c4(v=vs.110).aspx
All the projects had a Target CPU of AnyCPU. I changed the application project (the project that references all the other projects) to a Target CPU of x86. It now works.
所有项目都有一个 AnyCPU 的目标 CPU。我将应用程序项目(引用所有其他项目的项目)更改为 x86 的目标 CPU。它现在可以工作了。
Not sure how the Target CPU mix up occurred for no apparent reason, but it did.
不确定目标 CPU 混淆是如何无缘无故发生的,但确实发生了。
回答by fergal303
You may need to change the Appication Poolsetting "Enable 32bit Applications" to TRUE in IIS7 if you have at least 1 32bit dll\exe in your project.
如果您的项目中至少有 1 个 32 位 dll\exe,您可能需要在 IIS7中将应用程序池设置“启用 32 位应用程序”更改为 TRUE。
回答by Ali Adravi
Might be you are facing the problem with your website after deploying on server.
可能是您在服务器上部署后遇到网站问题。
Then you need to adjust your application pool to Enable 32-Bit Applications.
然后您需要调整您的应用程序池以启用 32 位应用程序。
Steps
脚步
- Open IIS Manager
- Click on Application Pools
- Select whatever application pool you are using
From right pane, click Advanced Settings...
Set Enable 32-Bit Applications to True
回答by Jawad Nadeem
I also face this problem in a project, after a few minutes i found the solution, this problem is due to CPU configuration, If you are using Visual Studio 2010 or VS 2013, just goto project 's propertiesand then select Compilefrom side bar and there will be 5 drop-down, 5th Drop-down will be Target CPU:, you should set it to x86 or x64according to your requirements instead of Any CPU.
我在一个项目中也遇到了这个问题,几分钟后我找到了解决方案,这个问题是由于 CPU 配置造成的,如果您使用的是Visual Studio 2010 或 VS 2013,只需转到项目的属性,然后从侧边栏中选择编译并且会有 5 个下拉列表,第 5 个下拉列表是Target CPU:,您应该根据您的要求将其设置为x86 或 x64,而不是 Any CPU。
My problem was solved after changing it to x86.
将其更改为 x86 后,我的问题解决了。
回答by G88
You might also see this issue if you're trying to package a 64bit project with an MSI installer in VS. ("The reason is because the native shim packaged with the .msi file is a 32-bit executable.")
如果您尝试在 VS 中使用 MSI 安装程序打包 64 位项目,您也可能会看到此问题。(“原因是因为与 .msi 文件一起打包的本机 shim 是 32 位可执行文件。”)
See here for more details: http://blogs.msdn.com/b/heaths/archive/2006/02/01/64-bit-managed-custom-actions-with-visual-studio.aspx
有关更多详细信息,请参见此处:http: //blogs.msdn.com/b/heaths/archive/2006/02/01/64-bit-managed-custom-actions-with-visual-studio.aspx
回答by Hovhannes Babayan
It can be a little funny, but I had the same problem with normal working code. I added StreamWriter and StreamReader and it gave that error. The solution was I took that code into comment brackets then did debug and it started to work again
这可能有点有趣,但我在正常工作代码中遇到了同样的问题。我添加了 StreamWriter 和 StreamReader,它给出了那个错误。解决方案是我将该代码放入注释括号中,然后进行调试并再次开始工作
回答by TTT
I just had this error message running IIS Express in Visual Studio 2015. In my case I needed to be running the 64 bit version of IIS Express:
我刚刚在 Visual Studio 2015 中运行 IIS Express 时收到此错误消息。就我而言,我需要运行 64 位版本的 IIS Express:
Tools → Options → Projects and Solutions → Web Projects
Check the box that says "Use the 64 bit version of IIS Express for web sites and projects".
工具 → 选项 → 项目和解决方案 → Web 项目
选中“为网站和项目使用 64 位版本的 IIS Express”框。
Screenshot:
截屏:


