Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Run-time Exception System.BadImageFormatException

Please help, I've tried everything else I can think of to solve this problem.

And before you respond please note:

I have done everything I can from other questions on StackOverflow.com and else-ware on the web. Such as but not limited to: Changing the build configuration from: "Any CPU" to "x64" and even to "x86". And also changing the target build from .NET 4.0 to .NET 3.5 (This does not work as I am using System.Windows.Interactivity that requires .NET 4.0) So I'm rather stuck with .NET 4.0. So please don't give an answer telling me to do this as I have already tried various combinations of this.


I've got a project in VS2013 called TimersXP that is an open-source project on CodePlex.com: https://timersxp.codeplex.com/

It builds without any errors, but I'm getting a run-time exception: 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.

A little history, the project was originally .NET 3.5, but when I found I had to add System.Windows.Interactivity and that had to support .NET 4.0 I bumped up the version number.

<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>

Yes I know it says version 4.5.0.0. I tried combinations of that as well. unless I missed some combination that works different that what would be expected.

It's open source so all of the code for the project is available, can someone please help me out? I'm afraid I am out of ideas.

Maybe in the App.config file this version number?

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

I don't want to just go through all of the code and change every place it says version to 3.5 or 4.0 or 4.5. That didn't seem like a very good idea.

As usual, once I see it, I'll probably want to kick myself!

like image 514
Seth Eden Avatar asked Feb 03 '14 02:02

Seth Eden


People also ask

How to resolve system badimageformatexception?

Restart your web server and try again. Additionally, you can change the settings on Visual Studio. In my case, I went to Tools > Options > Projects and Solutions > Web Projects and checked Use the 64 bit version of IIS Express for web sites and projects - This was on VS Pro 2015. Nothing else fixed it but this.

What is badimageformatexception?

The exception that is thrown when the file image of a dynamic link library (DLL) or an executable program is invalid.


2 Answers

It is strange that in my case, my project properties was already showing up as 4.5.2 but my app.config was still showing the runtime version as 2.0. I right clicked on project > chose project properties > updated target framework to 4.5.1 first and then updated to 4.5.2. That made the trick and updated the app.config as below:

Before:

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

After:

<startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" />
  </startup>
like image 100
user007 Avatar answered Sep 19 '22 09:09

user007


I had a similar problem as well with a super simple console app but mine turned out to be because it was relying on some libraries which were set to x86 only, and it wouldn't work on AnyCPU.

The fix: change my console app to also only build on x86 configuration and it worked.

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.

Screenshot of Exception message

Also see: Troubleshooting BadImageFormatException

like image 25
m1m1k Avatar answered Sep 22 '22 09:09

m1m1k