Share via


如何:判斷檔案是否為組件

檔案只有受管理時才是組件,而且其中繼資料會包含組件項目。 如需組件和中繼資料的詳細資訊,請參閱組件資訊清單

如何以手動方式判斷檔案是否為組件

  1. 啟動 Ildasm.exe(IL 反組譯)工具。

  2. 載入要測試的檔案。

  3. 如果 ILDASM 回報該檔案並非可攜式執行檔 (PE),則檔案不是組件。 如需詳細資訊,請參閱如何:檢視組件內容主題。

如何以程式設計方式判斷檔案是否為組件

使用 AssemblyName 類別

  1. 呼叫 AssemblyName.GetAssemblyName 方法,並傳遞您要測試之檔案的完整檔案路徑和名稱。

  2. 如果擲回 BadImageFormatException 例外狀況,則檔案不是組件。

此範例會測試一個 DLL 以查看其是否為組件。

using System;
using System.IO;
using System.Reflection;
using System.Runtime.InteropServices;

static class ExampleAssemblyName
{
    public static void CheckAssembly()
    {
        try
        {
            string path = Path.Combine(
                RuntimeEnvironment.GetRuntimeDirectory(),
                "System.Net.dll");

            AssemblyName testAssembly = AssemblyName.GetAssemblyName(path);
            Console.WriteLine("Yes, the file is an assembly.");
        }
        catch (FileNotFoundException)
        {
            Console.WriteLine("The file cannot be found.");
        }
        catch (BadImageFormatException)
        {
            Console.WriteLine("The file is not an assembly.");
        }
        catch (FileLoadException)
        {
            Console.WriteLine("The assembly has already been loaded.");
        }
    }

    /* Output: 
    Yes, the file is an assembly.  
    */
}
Imports System
Imports System.IO
Imports System.Reflection
Imports System.Runtime.InteropServices

Module ExampleAssemblyName
    Sub CheckAssembly()
        Try
            Dim filePath As String = Path.Combine(
                RuntimeEnvironment.GetRuntimeDirectory(),
                "System.Net.dll")

            Dim testAssembly As AssemblyName =
                                AssemblyName.GetAssemblyName(filePath)
            Console.WriteLine("Yes, the file is an Assembly.")
        Catch ex As FileNotFoundException
            Console.WriteLine("The file cannot be found.")
        Catch ex As BadImageFormatException
            Console.WriteLine("The file is not an Assembly.")
        Catch ex As FileLoadException
            Console.WriteLine("The Assembly has already been loaded.")
        End Try
    End Sub
End Module
' Output:  
' Yes, the file is an Assembly.  

GetAssemblyName 方法會載入測試檔案,然後在讀取資訊之後釋放它。

使用 PEReader 類別

  1. 如果您是以 .NET Standard 或 .NET Framework 為目標,請安裝 System.Reflection.Metadata NuGet 套件。 (以 .NET Core 或 .NET 5+ 為目標時,不需要此步驟,因為此程式庫包含在共用架構中。)

  2. 建立 System.IO.FileStream 執行個體,以從您要測試的檔案讀取資料。

  3. 建立 System.Reflection.PortableExecutable.PEReader 執行個體,將檔案串流傳遞至建構函式。

  4. 檢查 HasMetadata 屬性的值。 如果值為 false,則檔案不是組件。

  5. 在 PE 讀取器執行個體上呼叫 GetMetadataReader 方法,以建立中繼資料讀取器。

  6. 檢查 IsAssembly 屬性的值。 如果值為 true,則檔案是組件。

GetAssemblyName 方法不同,PEReader 類別不會在原生可攜式可執行檔 (PE) 檔案上擲回例外狀況。 這可讓您在需要檢查這類檔案時避免例外狀況所造成的額外效能成本。 如果檔案不存在或不是 PE 檔案,您仍然需要處理例外狀況。

此範例示範如何使用 PEReader 類別來判斷檔案是否為組件。

using System;
using System.Collections.Generic;
using System.IO;
using System.Reflection.Metadata;
using System.Reflection.PortableExecutable;
using System.Runtime.InteropServices;

static class ExamplePeReader
{
    static bool IsAssembly(string path)
    {
        using var fs = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);

        // Try to read CLI metadata from the PE file.
        using var peReader = new PEReader(fs);

        if (!peReader.HasMetadata)
        {
            return false; // File does not have CLI metadata.
        }

        // Check that file has an assembly manifest.
        MetadataReader reader = peReader.GetMetadataReader();
        return reader.IsAssembly;
    }

    public static void CheckAssembly()
    {
        string path = Path.Combine(
                RuntimeEnvironment.GetRuntimeDirectory(),
                "System.Net.dll");

        try
        {
            if (IsAssembly(path))
            {
                Console.WriteLine("Yes, the file is an assembly.");
            }
            else
            {
                Console.WriteLine("The file is not an assembly.");
            }
        }
        catch (BadImageFormatException)
        {
            Console.WriteLine("The file is not an executable.");
        }
        catch (FileNotFoundException)
        {
            Console.WriteLine("The file cannot be found.");
        }
    }

    /* Output: 
    Yes, the file is an assembly.  
    */
}
Imports System
Imports System.Collections.Generic
Imports System.IO
Imports System.Reflection.Metadata
Imports System.Reflection.PortableExecutable
Imports System.Runtime.InteropServices

Module ExamplePeReader
    Function IsAssembly(path As String) As Boolean

        Dim fs As FileStream = New FileStream(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)

        ' Try to read CLI metadata from the PE file.
        Dim peReader As PEReader = New PEReader(fs)
        Using (peReader)
            If Not peReader.HasMetadata Then
                Return False ' File does Not have CLI metadata.
            End If

            ' Check that file has an assembly manifest.
            Dim reader As MetadataReader = peReader.GetMetadataReader()
            Return reader.IsAssembly
        End Using
    End Function

    Sub CheckAssembly()
        Dim filePath As String = Path.Combine(
                RuntimeEnvironment.GetRuntimeDirectory(),
                "System.Net.dll")

        Try
            If IsAssembly(filePath) Then
                Console.WriteLine("Yes, the file is an assembly.")
            Else
                Console.WriteLine("The file is not an assembly.")
            End If
        Catch ex As BadImageFormatException
            Console.WriteLine("The file is not an executable.")
        Catch ex As FileNotFoundException
            Console.WriteLine("The file cannot be found.")
        End Try
    End Sub
End Module
' Output:  
' Yes, the file is an Assembly.

另請參閱