18

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

在您回复之前,请注意:

我已经从 StackOverflow.com 上的其他问题和网络上的 else-ware 中尽我所能。例如但不限于:将构建配置从:“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 未处理消息:未知模块中发生“System.BadImageFormatException”类型的未处理异常。附加信息:无法加载文件或程序集“TimersXP.exe”或其依赖项之一。此程序集由比当前加载的运行时更新的运行时构建,无法加载。

有点历史,该项目最初是 .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。这似乎不是一个好主意。

像往常一样,一旦我看到它,我可能想踢自己!

4

4 回答 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>
于 2015-06-23T15:52:12.213 回答
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 故障排除

于 2016-06-23T16:46:06.760 回答
5

我删除了

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

配置中的部分和应用程序工作。

我假设该声明在需要 2 和 4 时将应用程序限制为框架 2。

于 2017-09-25T09:30:12.443 回答
1

我在 Visual Studio 2022 中还有另一个对我有用的解决方案。在调试模式下启动应用程序时,我会得到 System.BadImageFormatException,但不是在发布模式下。

错误信息:

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.

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

在此处输入图像描述

于 2022-01-21T16:14:00.607 回答