259
C:\Windows\Microsoft.NET\Framework64\v4.0.30319>InstallUtil.exe C:\_PRODUKCIJA\D
ebug\DynamicHtmlTool.exe
Microsoft (R) .NET Framework Installation utility Version 4.0.30319.1
Copyright (c) Microsoft Corporation.  All rights reserved.

Exception occurred while initializing the installation:
System.BadImageFormatException: Could not load file or assembly 'file:///C:\_PRO
DUKCIJA\Debug\DynamicHtmlTool.exe' or one of its dependencies. An attempt was ma
de to load a program with an incorrect format..

C:\Windows\Microsoft.NET\Framework64\v4.0.30319>

service is x86 compiled even both computers are x64 and it works on my computer. Here in server where is win 2008 i get this error.

i try solutions from google but none works.

Like write here i have x86 project http://www.davesquared.net/2008/12/systembadimageformatexception-on-64-bit.html

Ruben Bartelink
  • 59,778
  • 26
  • 187
  • 249
senzacionale
  • 20,448
  • 67
  • 204
  • 316

5 Answers5

303

It seems that you are using the 64-bit version of the tool to install a 32-bit/x86 architecture application. Look for the 32-bit version of the tool here:

C:\Windows\Microsoft.NET\Framework\v4.0.30319

and it should install your 32-bit application just fine.

Sizons
  • 640
  • 2
  • 8
  • 24
madd0
  • 9,053
  • 3
  • 35
  • 62
  • 63
    or change in Visual Studio(compiler settings) the target from “X86 ” to “Any CPU”. – Tim Schmelter Oct 25 '12 at 15:07
  • @TimSchmelter I wish I would have seen it here first. That is what I did and it worked (changed the target from Any CPU in my case to match my referenced project - x64). – FernandoZ Aug 25 '20 at 16:42
  • For me, I had selected x86 for my debug target... twice (I blame my dyslexia) – Peter L Apr 21 '21 at 15:52
  • Note: Ran into the same error with .Net Framework libraries referenced from .Net Standard when the CPU targets were not set to be the same. So this error still very much exists in recent .Net releases. – Jeff Jul 17 '23 at 20:48
155

Try to configure the setting of your projects, it is usually due to x86/x64 architecture problems:

Go and set your choice as shown:

Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
David
  • 15,894
  • 22
  • 55
  • 66
129

I found a different solution to this issue. Apparently my IIS 7 did not have 32bit mode enabled in my Application Pool by default.

To enable 32bit mode, open IIS and select your Application Pool. Mine was named "ASP.NET v4.0".
Right click, go to "Advanced Settings" and change the section named: "Enabled 32-bit Applications" to true.

Restart your web server and try again.

I found the fix from this blog reference: http://darrell.mozingo.net/2009/01/17/running-iis-7-in-32-bit-mode/

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.

Joseph Snow
  • 2,436
  • 1
  • 21
  • 22
6

I had the same exception installing using correct framework.

My solution was running cmd as administrator .... then it worked fine.

woony
  • 240
  • 4
  • 8
  • Strange, if you have more detail or a rationale, no harm to stick it on http://stackoverflow.com/questions/323140/system-badimageformatexception-incorrect-format-when-trying-to-install-service?lq=1 (But I'd downvote it unless there is a rationale as this doesnt make sense to me :P) – Ruben Bartelink May 15 '13 at 20:49
  • I never stated I could explain why, because I can not. My dots .... might have indicated the fact , that it didn't make any sense for me aswell. But from experience that is not uncommon using anything microsoft. – woony May 17 '13 at 11:32
  • I normally find that stuff that doesn't make sense doesn't make sense for a reason and PEIMS is easier than PEBKAC. It is my contention that you did more than just this single thing different as this makes no sense. The error is a 64/32 bit conflict and running as Admin can't influence that without some mad path redirections or profiles confusing matters. But it's highly unlikely so I dont think this is a helpful answer for anyone so -1 – Ruben Bartelink May 17 '13 at 11:56
  • 2
    @RubenBartelink, if Woony had a workaround, that helped him, it's good to share, even if he can't explain why. It's not a reason to down-vote, in particular when there are other high-voted answers – Michael Freidgeim May 20 '13 at 20:05
  • @MichaelFreidgeim And if he ran around his house/apartment block three times and then rebooted and that fixed it? Would *you* upvote the answer if it had 0 votes instead of -1 ? It doesn't really make sense when you think about it. – Ruben Bartelink May 20 '13 at 20:13
  • 1
    I've upvoted only to compensate your downvote. Be nice to new users. Btw, reboot often helps, but I never tried to run more than twice around the apartment block :). – Michael Freidgeim May 20 '13 at 20:21
  • :) I've had this same debate with others before (normally on the 'relax with the rules' stance). I know it's nice to be nice but we'll have to agree to disagree on the 'try this thing I can't explain'. And coincidentally today, I asked for a reopen http://stackoverflow.com/questions/16644146/eventstore-commit-guid-what-is-it-really-used-for (new user, but key thing:- a good question) but had another such 'debate':- http://stackoverflow.com/questions/329355/cannot-delete-directory-with-directory-deletepath-true/14933880#comment23947114_7518831 - Do you agree with that one too :P – Ruben Bartelink May 20 '13 at 21:16
  • 4
    This is a tad old.... But in Woony's defense..... 9 months later, and this is what solved my issue. – Johnrad Jan 15 '14 at 19:31
3

My cause was different I referenced a web service then I got this message.

Then I changed my target .Net Framework 4.0 to .Net Framework 2.0 and re-refer my webservice. After a few changes problem solved. There is no error worked fine.

hope this helps!

Lucky
  • 16,787
  • 19
  • 117
  • 151
Davut Gürbüz
  • 5,526
  • 4
  • 47
  • 83