Run-time Exception System.BadImageFormatException

20,818

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

Share:
20,818

Related videos on Youtube

Seth Eden
Author by

Seth Eden

I could describe myself in one word "visionary", but that might be understated. My smile never ends. I am a team player, and I love to be challenged. My experience and skills are broad and deep as well. I enjoy technically challenging jobs. I have set high goals for myself; I intend to achieve them. For the company that hires me as their employee, I will be their best worker because I am easy to get along with, and I have high ethical work standards. It is my desire to represent my employer as a producer of excellence in the marketplace. Thank you for consideration of my resume &amp; Portfolio. Please take you're time to explore and enjoy.

Updated on January 22, 2022

Comments

  • Seth Eden
    Seth Eden over 2 years

    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!

    • T.S.
      T.S. over 10 years
      Run corflags on all participating assemblies and you will quickly see, which one is of wrong format
    • Seth Eden
      Seth Eden over 10 years
      In-work. Will post results. Thanks
    • Alexei Levenkov
      Alexei Levenkov over 10 years
      Recompiling everything to 4.5 would be easiest solution... But since it does not work for you - make sure exe uses correct run-time (max of all necessary versions, probably 4.5) than see if any other assemblies still need to be convinced to load (native ones may need an extra touch...)
    • Mike Zboray
      Mike Zboray over 10 years
      You need to load the proper version of the CLR. The app config supported runtime is version 2.0. You need to change it to use 4.0. See this for details on the proper configuration.
    • Seth Eden
      Seth Eden over 10 years
      Thanks Alexie Levenkov, that's what I was thinking too, currently set to 4.5. I'll continue to try and finagle with it.
    • Seth Eden
      Seth Eden over 10 years
      Hey Mike Z That sort of did it. Not throwing an exception any more! Now it just exits with code -2146232576 (0x80131700)
    • Mike Zboray
      Mike Zboray over 10 years
      You could just delete the app config as well, since you don't have anything else in it.
    • Seth Eden
      Seth Eden over 10 years
      Ok I checked in the updated files, so no exception just exits with the above error code.
    • Seth Eden
      Seth Eden over 10 years
      BINGO!!! Deleting App.Config did it! THANK YOU SOOOO MUCH!!!
    • Seth Eden
      Seth Eden over 10 years
      Ok back in business! :-D Thanks again!
    • Seth Eden
      Seth Eden over 10 years
      Thanks Will. Deleted. ;-)
    • Seth Eden
      Seth Eden over 10 years
      Hey Mike Z, would you mind converting your solution into an answer so we can close this question down? Thanks.
  • karlis
    karlis over 3 years
    If you do not move the app config after initial install (meaning in an update), you stick to that problem until you find this one.... Thank you.