运行时异常 System.BadImageFormatException

18

请帮忙,我已经尝试了所有我能想到的方法来解决这个问题。

在您回复之前,请注意:

我已经从StackOverflow.com和其他网站上做了我能做的一切。例如但不限于: 将构建配置从"Any CPU"更改为"x64"甚至"x86"。还将目标构建从.NET 4.0更改为.NET 3.5(这行不通,因为我使用了需要.NET 4.0的System.Windows.Interactivity)。所以我被困在.NET 4.0中。因此,请不要给我提供一个让我这样做的答案,因为我已经尝试过各种组合。


我在VS2013中有一个名为TimersXP的项目,它是CodePlex.com上的一个开源项目: https://timersxp.codeplex.com/

它编译时没有任何错误,但是我遇到了运行时异常: System.BadImageFormatException was unhandled Message: An unhandled exception of type 'System.BadImageFormatException' occurred in Unknown Module. Additional information: Could not load file or assembly 'TimersXP.exe' or one of its dependencies. This assembly is built by a runtime newer than the currently loaded runtime and cannot be loaded.

稍微说一下历史,这个项目最初是.NET 3.5,但当我发现我必须添加System.Windows.Interactivity并且必须支持.NET 4.0时,我增加了版本号。

<Reference Include="System.Windows.Interactivity, Version=4.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
  <SpecificVersion>False</SpecificVersion>
  <HintPath>C:\Program Files (x86)\Microsoft SDKs\Expression\Blend\.NETFramework\v4.0\Libraries\System.Windows.Interactivity.dll</HintPath>
  <Private>False</Private>
</Reference>

是的,我知道它显示的是版本4.5.0.0,我也尝试过不同的组合,除非我错过了一些意料之外的组合方法。

这是开源的,所以整个项目的代码都可以获得,有人能帮帮我吗?我已经没有其他想法了。

也许在App.config文件中可以找到这个版本号?

<?xml version="1.0"?>
<configuration>
<startup><supportedRuntime version="v2.0.50727"/></startup></configuration>

我不想只是遍历所有代码并更改每处提到版本号的地方为3.5或4.0或4.5。那似乎不是一个很好的主意。

通常,一旦我看到它,我可能会想打自己的脸!


1
运行 corflags 在所有参与的程序集上,您很快就会看到哪一个格式不正确。 - T.S.
5
您需要加载正确版本的CLR。应用程序配置所支持的运行时版本是2.0。您需要将其更改为使用4.0。请参见此链接以了解正确配置的详细信息:http://msdn.microsoft.com/en-us/library/w4atty68(v=vs.110).aspx - Mike Zboray
嗨,Mike Z,那差不多就是这样了。不再抛出异常了!现在只是以代码-2146232576(0x80131700)退出。 - Seth Eden
1
你也可以删除应用程序配置,因为它里面没有其他内容。 - Mike Zboray
1
嗨,Mike Z,你能把你的解决方案转换成答案吗?这样我们就可以关闭这个问题了。谢谢。 - Seth Eden
显示剩余7条评论
4个回答

16

在我的情况下,奇怪的是,我的项目属性已经显示为4.5.2,但我的app.config仍然显示运行时版本为2.0。我右键单击项目>选择项目属性>首先将目标框架更新为4.5.1,然后再更新为4.5.2。这样就解决了问题,并更新了app.config如下:

之前:

<startup>
    <supportedRuntime version="v2.0.50727"/>
  </startup>

之后:

<startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" />
  </startup>

如果您在初始安装后不移动应用程序配置(即在更新中),则会一直遇到该问题,直到找到解决方法......谢谢。 - karlis

11

我也遇到了一个类似的问题,我的控制台应用程序非常简单,但问题在于它依赖于一些只设置为 x86 的库,在 AnyCPU 上无法工作。

解决方法:将我的控制台应用程序更改为仅在 x86 配置上构建,然后它就可以工作了。

System.BadImageFormatException was unhandled
Message: An unhandled exception of type 'System.BadImageFormatException'     occurred in Unknown Module.
Additional information: Could not load file or assembly 'My.Assembly,     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故障排除


5
我已经删除了

标签。

<startup>
    <supportedRuntime version="v2.0.50727"/>
</startup>

我认为这个声明限制了应用程序使用框架2,而实际上它需要2和4两个版本。

配置文件中的部分和应用程序正常运行。


1

我有另一个解决方案适用于Visual Studio 2022。在Debug模式下启动应用程序时,我会遇到System.BadImageFormatException错误,但在Release模式下则不会。

错误信息:

An unhandled exception of type 'System.BadImageFormatException' occurred in Unknown Module.
Could not load file or assembly 'Test.exe' or one of its dependencies. An attempt was made to load a program with an incorrect format.

解决方案:项目属性 → 调试 → 勾选 启用本机代码调试 选项

enter image description here


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接