I have two projects, ProjectA
and ProjectB
. 我有两个项目, ProjectA
和ProjectB
。 ProjectB
is a console application, which depends on ProjectA
. ProjectB
是一个控制台应用程序,它依赖于ProjectA
。 Yesterday, everything was working fine, but suddenly today when I run ProjectB
I get this: 昨天,一切正常,但是今天突然当我运行ProjectB
我得到了:
BadImageFormatException was unhandled : BadImageFormatException未处理 :
Could not load file or assembly 'ProjectA, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' or one of its dependencies. 无法加载文件或程序集'ProjectA,版本= 1.0.0.0,区域性=中性,PublicKeyToken =空'或其依赖项之一。 An attempt was made to load a program with an incorrect format. 试图加载格式错误的程序。
Both are just regular projects, with no dependencies on any other non-.Net projects. 两者都是常规项目,不依赖于任何其他非.Net项目。 Both are fully .Net - there is no native code, and no P/Invoke. 两者都是完全.Net-没有本机代码,也没有P / Invoke。 I have other projects which depend on ProjectA
and still work just fine. 我还有其他依赖ProjectA
,但仍然可以正常工作。
Things I have tried: 我尝试过的事情:
- Make sure both projects are set to "Any CPU," with the build checkbox checked. 确保两个项目都设置为“ Any CPU”,并且选中了构建复选框。 They are. 他们是。
- Make sure both projects are for the same Target Framework (.Net 4.0 Client Profile) . 确保两个项目都针对相同的目标框架(.Net 4.0客户端配置文件) 。
- Under ProjectB --> References --> ProjectA --> Properties, make sure "Copy Local" is set to "True" _ (I verified that ProjectA.dll is being copied correctly) 在ProjectB->引用-> ProjectA->属性下,确保将“ Copy Local”设置为“ True” _ (我已验证ProjectA.dll是否已正确复制)
- Clean/Rebuild the solution. 清洁/重建解决方案。 I even tried manually deleting the /bin and /obj folders in both projects. 我什至尝试在两个项目中手动删除/ bin和/ obj文件夹。
- Restart Visual Studio. 重新启动Visual Studio。 Restart my computer. 重新启动计算机。
- Check out an entirely new copy of the repository. 签出存储库的全新副本。
But I still get the same error. 但是我仍然遇到同样的错误。 I have no idea what I did to cause this, nor how to fix it. 我不知道该怎么做,也不知道如何解决。 Any ideas? 有任何想法吗?
#1楼
参考:https://stackoom.com/question/lhwe/无法加载文件或程序集-试图加载格式不正确的程序-System-BadImageFormatException
#2楼
I am pretty sure you're having a 32-bit / 64-bit conflict. 我很确定您遇到32位/ 64位冲突。 It sounds like your main project might be set to 32-bit while the class its referencing is set to 64-bit. 听起来您的主项目可能设置为32位,而其引用的类设置为64位。 Try looking at this SO question and this one too . 尝试看这个SO问题 , 也是 这个问题 。 Between the two of them, you should be able to figure out your problem. 在这两者之间,您应该能够找出问题所在。
#3楼
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". 我已经将项目A的“平台目标”(“项目A”(右键单击)->“属性”->“构建”->“平台目标”)设置为x86,但将项目B的目标保持在“任何CPU”。 Setting Project B's to "x86" fixed this. 将项目B设置为“ x86”可解决此问题。
#4楼
在同一个解决方案中的多个项目中,我遇到了同样的问题,最终我将所有目标框架都设置为目标CPU的.NET Framework 4和x86,并最终成功编译。
#5楼
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: 在Exception快照中,对于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/zh-cn/library/e74a18c4(v=vs.110).aspx
All the projects had a Target CPU of AnyCPU. 所有项目的目标CPU均为AnyCPU。 I changed the application project (the project that references all the other projects) to a Target CPU of x86. 我将应用程序项目(引用所有其他项目的项目)更改为x86的目标CPU。 It now works. 现在可以使用了。
Not sure how the Target CPU mix up occurred for no apparent reason, but it did. 不确定目标CPU是如何混合的,没有明显原因,但是确实如此。
#6楼
如果您的项目中至少有1个32bit dll \\ exe,则可能需要在IIS7中将“应用程序池”设置“启用32位应用程序”更改为TRUE。