BadImageFormatException Class


The .NET API Reference documentation has a new home. Visit the .NET API Browser on to see the new experience.

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

Namespace:   System
Assembly:  mscorlib (in mscorlib.dll)


public class BadImageFormatException : SystemException


Initializes a new instance of the BadImageFormatException class.

System_CAPS_protmethodBadImageFormatException(SerializationInfo, StreamingContext)

Initializes a new instance of the BadImageFormatException class with serialized data.


Initializes a new instance of the BadImageFormatException class with a specified error message.

System_CAPS_pubmethodBadImageFormatException(String, Exception)

Initializes a new instance of the BadImageFormatException class with a specified error message and a reference to the inner exception that is the cause of this exception.

System_CAPS_pubmethodBadImageFormatException(String, String)

Initializes a new instance of the BadImageFormatException class with a specified error message and file name.

System_CAPS_pubmethodBadImageFormatException(String, String, Exception)

Initializes a new instance of the BadImageFormatException class with a specified error message and a reference to the inner exception that is the cause of this exception.


Gets a collection of key/value pairs that provide additional user-defined information about the exception.(Inherited from Exception.)


Gets the name of the file that causes this exception.


Gets the log file that describes why an assembly load failed.


Gets or sets a link to the help file associated with this exception.(Inherited from Exception.)


Gets or sets HRESULT, a coded numerical value that is assigned to a specific exception.(Inherited from Exception.)


Gets the Exception instance that caused the current exception.(Inherited from Exception.)


Gets the error message and the name of the file that caused this exception.(Overrides Exception.Message.)


Gets or sets the name of the application or the object that causes the error.(Inherited from Exception.)


Gets a string representation of the immediate frames on the call stack.(Inherited from Exception.)


Gets the method that throws the current exception.(Inherited from Exception.)


Determines whether the specified object is equal to the current object.(Inherited from Object.)


Allows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection.(Inherited from Object.)


When overridden in a derived class, returns the Exception that is the root cause of one or more subsequent exceptions.(Inherited from Exception.)


Serves as the default hash function. (Inherited from Object.)

System_CAPS_pubmethodGetObjectData(SerializationInfo, StreamingContext)

Sets the SerializationInfo object with the file name, assembly cache log, and additional exception information.(Overrides Exception.GetObjectData(SerializationInfo, StreamingContext).)


Gets the runtime type of the current instance.(Inherited from Exception.)


Creates a shallow copy of the current Object.(Inherited from Object.)


Returns the fully qualified name of this exception and possibly the error message, the name of the inner exception, and the stack trace.(Overrides Exception.ToString().)


Occurs when an exception is serialized to create an exception state object that contains serialized data about the exception.(Inherited from Exception.)

This exception is thrown when the file format of a dynamic link library (.dll file) or an executable (.exe file) doesn’t conform to the format that the common language runtime expects. In particular, the exception is thrown under the following conditions:

  • An earlier version of a .NET Framework utility, such as ILDasm.exe or installutil.exe, is used with an assembly that was developed with a later version of the .NET Framework.

    To address this exception, use the version of the tool that corresponds to the version of the .NET Framework that was used to develop the assembly. This may require modifying the Path environment variable or providing a fully qualified path to the correct executable.

  • You try to load an unmanaged dynamic link library or executable (such as a Windows system DLL) as if it were a .NET Framework assembly. The following example illustrates this by using the Assembly.LoadFile method to load Kernel32.dll.

    // Windows DLL (non-.NET assembly)
    string filePath = Environment.ExpandEnvironmentVariables("%windir%");
    if (! filePath.Trim().EndsWith(@"\"))
       filePath += @"\";
    filePath += @"System32\Kernel32.dll";
    try {
       Assembly assem = Assembly.LoadFile(filePath);
    catch (BadImageFormatException e) {
       Console.WriteLine("Unable to load {0}.", filePath);
                         e.Message.IndexOf(".") + 1));   
    // The example displays an error message like the following:
    //       Unable to load C:\WINDOWS\System32\Kernel32.dll.
    //       The module was expected to contain an assembly manifest.

    To address this exception, access the methods defined in the DLL by using the features provided by your development language, such as the Declare statement in Visual Basic or the DllImportAttribute attribute with the extern keyword in C#.

  • A DLL or executable is loaded as a 64-bit assembly, but it contains 32-bit features or resources. For example, it relies on COM interop or calls methods in a 32-bit dynamic link library.

    To address this exception, set the project's Platform target property to x86 (instead of x64 or AnyCPU) and recompile.

  • Your application’s components were created using different versions of the .NET Framework. Typically, this exception occurs when an application or component that was developed using the .NET Framework 1.0 or the .NET Framework 1.1 tries to load an assembly that was developed using the .NET Framework 2.0 SP1 or later, or when an application that was developed using the .NET Framework 2.0 SP1 or .NET Framework 3.5 tries to load an assembly that was developed using the .NET Framework 4 or later. The BadImageFormatException may be reported as a compile-time error, or the exception may be thrown at run time. The following example defines a StringLib class that has a single member, ToProperCase, and that resides in an assembly named StringLib.dll.

    using System;
    public class StringLib
       private string[] exceptionList = { "a", "an", "the", "in", "on", "of" };
       private char[] separators = { ' ' };
       public string ToProperCase(string title)
          bool isException = false;	
          string[] words = title.Split( separators, StringSplitOptions.RemoveEmptyEntries);
          string[] newWords = new string[words.Length];
          for (int ctr = 0; ctr <= words.Length - 1; ctr++)
             isException = false;
             foreach (string exception in exceptionList)
                if (words[ctr].Equals(exception) && ctr > 0)
                   isException = true;
             if (! isException)
                newWords[ctr] = words[ctr].Substring(0, 1).ToUpper() + words[ctr].Substring(1);
                newWords[ctr] = words[ctr];	 
          return String.Join(" ", newWords); 			
    // Attempting to load the StringLib.dll assembly produces the following output:
    //    Unhandled Exception: System.BadImageFormatException: 
    //                         The format of the file 'StringLib.dll' is invalid.

    The following example uses reflection to load an assembly named StringLib.dll. If the source code is compiled with a .NET Framework 1.1 compiler, a BadImageFormatException is thrown by the Assembly.LoadFrom method.

    using System;
    using System.Reflection;
    public class Example
       public static void Main()
          string title = "a tale of two cities";
    //      object[] args = { title}
          // Load assembly containing StateInfo type.
          Assembly assem = Assembly.LoadFrom(@".\StringLib.dll");
          // Get type representing StateInfo class.
          Type stateInfoType = assem.GetType("StringLib");
          // Get Display method.
          MethodInfo mi = stateInfoType.GetMethod("ToProperCase");
          // Call the Display method. 
          string properTitle = (string) mi.Invoke(null, new object[] { title } );

    To address this exception, make sure that the assembly whose code is executing and that throws the exception, and the assembly to be loaded both target compatible versions of the .NET Framework.

  • The components of your application target different platforms. For example, you are trying to load ARM assemblies in an x86 application. You can use the following command-line utility to determine the target platforms of individual .NET Framework assemblies. The list of files should be provided as a space-delimited list at the command line.

    using System;
    using System.IO;
    using System.Reflection;
    public class Example
       public static void Main()
          String[] args = Environment.GetCommandLineArgs();
          if (args.Length == 1) {
             Console.WriteLine("\nSyntax:   PlatformInfo <filename>\n");
          // Loop through files and display information about their platform.
          for (int ctr = 1; ctr < args.Length; ctr++) {
             string fn = args[ctr];
             if (! File.Exists(fn)) {
                Console.WriteLine("File: {0}", fn);
                Console.WriteLine("The file does not exist.\n");
             else {
                try {
                   AssemblyName an = AssemblyName.GetAssemblyName(fn);
                   Console.WriteLine("Assembly: {0}", an.Name);
                   if (an.ProcessorArchitecture == ProcessorArchitecture.MSIL)
                      Console.WriteLine("Architecture: AnyCPU");
                      Console.WriteLine("Architecture: {0}", an.ProcessorArchitecture);
                catch (BadImageFormatException) {
                   Console.WriteLine("File: {0}", fn);
                   Console.WriteLine("Not a valid assembly.\n");
  • Reflecting on C++ executable files may throw this exception. This is most likely caused by the C++ compiler stripping the relocation addresses or the .Reloc section from the executable file. To preserve the .relocation address in a C++ executable file, specify /fixed:no when linking.

BadImageFormatException uses the HRESULT COR_E_BADIMAGEFORMAT, which has the value 0x8007000B.

For a list of initial property values for an instance of BadImageFormatException, see the BadImageFormatException constructors.

Universal Windows Platform
Available since 8
.NET Framework
Available since 1.1
Portable Class Library
Supported in: portable .NET platforms
Available since 2.0
Windows Phone Silverlight
Available since 7.0
Windows Phone
Available since 8.1

Any public static ( Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Return to top