The strange case of System.BadImageFormatException: Could not load file or assembly ‘Microsoft.mshtml’

A couple of days ago another customer of my MZ-Tools add-in reported this exception:

“System.BadImageFormatException: Could not load file or assembly ‘Microsoft.mshtml, Version=7.0.3300.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a’ or one of its dependencies. The module was expected to contain an assembly manifest.”

The GAC (C:\Windows\Assembly folder) showed the file with the correct version and public key token, so what could it be?

As you may know, what you see in the Windows Explorer when you go to the C:\Windows\Assembly folder is not exactly the contents of that folder but a shell, and fortunately I am old enough to know the old MS-DOS commands to see the actual contents of a folder:

  • Click the Start, Run… menu
  • Type CMD and enter to open a MS-DOS window
  • Type CD C:\Windows\Assembly (CD stands for Change Directory)
  • Type DIR to list the contents
  • Type CD GAC
  • Type DIR to list the contents
  • Type CD Microsoft.MSHtml
  • Type DIR to list the contents
  • Type CD 7.0.3300.0__b03f5f7f11d50a3a
  • Type DIR to list the contents
  • You should get something like (it is in Spanish but I have marked in bold the relevant information):

El volumen de la unidad C no tiene etiqueta.
El número de serie del volumen es: 88B8-2ADF

Directorio de C:\WINDOWS\assembly\GAC\Microsoft.mshtml\7.0.3300.0__b03f5f7f11d50a3a

22/11/2007  11:16    <DIR>          .
22/11/2007  11:16    <DIR>          ..
22/11/2007  11:08         8.007.680 Microsoft.mshtml.dll
22/11/2007  11:16               207 __AssemblyInfo__.ini
2 archivos      8.007.887 bytes
2 dirs  16.842.235.904 bytes libres

So, the size of Microsoft.mshtml.dll should be 8.007.680 bytes, but for the customer was 0 bytes, so there was a file corruption. A repair of Visual Studio didn’t solve the problem so he had to delete the file with the command DEL Microsoft.mshtml.dll and then with a good copy of the file install it in the GAC (you can use the gacutil.exe command-line utility or drag and drop the file using the Windows Explorer).