Run-time Exception System.BadImageFormatException

Solution 1:

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>

Solution 2:

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

Solution 3:

i removed the

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

section from the config and the app worked.

i assume that statement was restricting the app to framework 2 when it required 2 and 4.

Solution 4:

I have yet another solution that worked for me in Visual Studio 2022. I'd get System.BadImageFormatException when starting application in Debug mode, but not in Release mode.

Error message:

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.

Solution: Project properties → Debug → Tick Enable native code debugging option

enter image description here