閱讀英文

共用方式為


BadImageFormatException 類別

定義

當動態連結程式庫 (DLL) 或可執行程式的檔案映像無效時擲回的例外狀況。

C#
public class BadImageFormatException : Exception
C#
public class BadImageFormatException : SystemException
C#
[System.Serializable]
public class BadImageFormatException : SystemException
C#
[System.Serializable]
[System.Runtime.InteropServices.ComVisible(true)]
public class BadImageFormatException : SystemException
繼承
BadImageFormatException
繼承
BadImageFormatException
屬性

備註

當動態連結程式庫 (.dll 檔案的檔案格式) 或可執行檔 (.exe 檔案) 不符合 Common Language Runtime 預期的格式時,就會擲回這個例外狀況。 特別是,例外狀況會在下列情況下擲回:

  • 舊版的 .NET 公用程式,例如 ILDasm.exe 或 installutil.exe,會與以較新版本 .NET 開發的元件搭配使用。

    若要解決這個例外狀況,請使用對應至用來開發元件的 .NET 版本的工具版本。 這可能需要修改 Path 環境變數,或提供正確可執行檔的完整路徑。

  • 您嘗試載入非受控動態連結程式庫或可執行檔 (,例如Windows系統 DLL) ,就像是 .NET 元件一樣。 下列範例說明如何使用 Assembly.LoadFile 方法來載入Kernel32.dll。

    C#
    // 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);
       Console.WriteLine(e.Message.Substring(0,
                         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.
    

    若要解決這個例外狀況,請使用開發語言所提供的功能來存取 DLL 中定義的方法,例如 Declare Visual Basic 中的 語句,或使用 DllImportAttribute C# 和 F# 中的 關鍵字來存取 屬性 extern

  • 您嘗試在僅限反映的內容中載入參考元件。 您可以使用下列兩種方式之一來解決此問題:

    • 您可以載入實作元件,而不是參考元件。
    • 您可以藉由呼叫 Assembly.ReflectionOnlyLoad 方法,在僅限反映的內容中載入參考元件。
  • DLL 或可執行檔會載入為 64 位元件,但它包含 32 位的功能或資源。 例如,它依賴 COM Interop 或呼叫 32 位動態連結程式庫中的方法。

    若要解決此例外狀況,請將專案的 [平臺目標 ] 屬性設定為 x86 (,而不是 x64 或 AnyCPU) 並重新編譯。

  • 您的應用程式元件是使用不同的 .NET 版本所建立。 一般而言,當使用 .NET Framework 1.0 或 .NET Framework 1.1 開發的應用程式或元件嘗試載入使用 .NET Framework 2.0 SP1 或更新版本開發的元件,或是使用 .NET Framework 2.0 SP1 或更新版本開發的應用程式時,就會發生這個例外狀況.NET Framework 3.5 嘗試載入使用 .NET Framework 4 或更新版本開發的元件。 BadImageFormatException可能會回報為編譯時期錯誤,或例外狀況可能會在執行時間擲回。 下列範例會定義類別,該類別具有單一 StringLib 成員 , ToProperCase 且位於名為 StringLib.dll 的元件中。

    C#
    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;
                   break;
                }
             }
    
             if (! isException)
                newWords[ctr] = words[ctr].Substring(0, 1).ToUpper() + words[ctr].Substring(1);
             else
                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.
    

    下列範例會使用反映來載入名為 StringLib.dll 的元件。 如果使用 .NET Framework 1.1 編譯器編譯原始程式碼, BadImageFormatException 方法會擲回 Assembly.LoadFrom

    C#
    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 } );
          Console.WriteLine(properTitle);
       }
    }
    

    若要解決這個例外狀況,請確定執行程式碼且擲回例外狀況的元件,以及要載入的元件,這兩個目標都是 .NET 相容版本。

  • 應用程式的元件以不同的平臺為目標。 例如,您嘗試在 x86 應用程式中載入 ARM 元件。 您可以使用下列命令列公用程式來判斷個別 .NET 元件的目標平臺。 檔案清單應該以以空格分隔清單的形式提供于命令列。

    C#
    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");
             return;
          }
          Console.WriteLine();
    
          // 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");
                   else
                      Console.WriteLine("Architecture: {0}", an.ProcessorArchitecture);
    
                   Console.WriteLine();
                }
                catch (BadImageFormatException) {
                   Console.WriteLine("File: {0}", fn);
                   Console.WriteLine("Not a valid assembly.\n");
                }
             }
          }
       }
    }
    
  • 反映 C++ 可執行檔時可能會擲回這個例外狀況。 這個情況最可能的原因,是因為 C++ 編譯器移除執行檔中的重新定位位置或 .Reloc 區段。 若要在 C++ 執行檔中保留重新配置位址,請在連接時指定 /fixed:no 。

BadImageFormatException 會使用 HRESULT COR_E_BADIMAGEFORMAT ,其值為 0x8007000B。

如需執行個體的初始屬性值的清單BadImageFormatException,請參閱BadImageFormatException建構函式。

建構函式

BadImageFormatException()

初始化 BadImageFormatException 類別的新執行個體。

BadImageFormatException(SerializationInfo, StreamingContext)

使用序列化資料,初始化 BadImageFormatException 類別的新執行個體。

BadImageFormatException(String)

使用指定的錯誤訊息,初始化 BadImageFormatException 類別的新執行個體。

BadImageFormatException(String, Exception)

使用指定的錯誤訊息以及造成此例外狀況的內部例外狀況的參考,初始化 BadImageFormatException 類別的新執行個體。

BadImageFormatException(String, String)

使用指定的錯誤訊息和檔案名稱,初始化 BadImageFormatException 類別的新執行個體。

BadImageFormatException(String, String, Exception)

使用指定的錯誤訊息以及造成此例外狀況的內部例外狀況的參考,初始化 BadImageFormatException 類別的新執行個體。

屬性

Data

取得鍵值組的集合,這些鍵值組會提供關於例外狀況的其他使用者定義資訊。

(繼承來源 Exception)
FileName

取得造成這個例外狀況的檔案名稱。

FusionLog

取得描述為什麼組件載入失敗的記錄檔。

HelpLink

取得或設定與這個例外狀況相關聯的說明檔連結。

(繼承來源 Exception)
HResult

取得或設定 HRESULT,它是指派給特定例外狀況的編碼數值。

(繼承來源 Exception)
InnerException

取得造成目前例外狀況的 Exception 執行個體。

(繼承來源 Exception)
Message

取得造成這個例外狀況的錯誤訊息和檔案名稱。

Source

取得或設定造成錯誤的應用程式或物件的名稱。

(繼承來源 Exception)
StackTrace

取得呼叫堆疊上即時運算框架的字串表示。

(繼承來源 Exception)
TargetSite

取得擲回目前例外狀況的方法。

(繼承來源 Exception)

方法

Equals(Object)

判斷指定的物件是否等於目前的物件。

(繼承來源 Object)
GetBaseException()

在衍生類別中覆寫時,傳回一或多個後續的例外狀況的根本原因 Exception

(繼承來源 Exception)
GetHashCode()

做為預設雜湊函式。

(繼承來源 Object)
GetObjectData(SerializationInfo, StreamingContext)

使用檔案名稱、組件快取記錄檔和額外的例外狀況資訊,來設定 SerializationInfo 物件。

GetObjectData(SerializationInfo, StreamingContext)

在衍生類別中覆寫時,使用例外狀況的資訊設定 SerializationInfo

(繼承來源 Exception)
GetType()

取得目前執行個體的執行階段類型。

(繼承來源 Exception)
MemberwiseClone()

建立目前 Object 的淺層複製。

(繼承來源 Object)
ToString()

傳回這個例外狀況的完整名稱、可能的錯誤訊息、內部例外的名稱和堆疊追蹤。

事件

SerializeObjectState
已過時。

當例外狀況序列化,以建立包含例外狀況相關序列化資料的例外狀況狀態物件時,就會發生此事件。

(繼承來源 Exception)

適用於

產品 版本
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0

另請參閱