Megosztás:


Útmutató: Annak meghatározása, hogy egy fájl szerelvény-e

A fájl akkor és csak akkor szerelvény, ha felügyelt, és tartalmaz egy szerelvénybejegyzést a metaadataiban. A szerelvényekről és a metaadatokról további információt a szerelvényjegyzékben talál.

Hogyan állapítható meg manuálisan, hogy egy fájl szerelvény-e

  1. Indítsa el a Ildasm.exe (IL-szétszerelt) eszközt.

  2. Töltse be a tesztelni kívánt fájlt.

  3. Ha az ILDASM azt jelenti, hogy a fájl nem hordozható végrehajtható (PE) fájl, akkor nem szerelvény. További információ: Útmutató: Szerelvény tartalmának megtekintése.

Hogyan határozható meg programozott módon, hogy egy fájl szerelvény-e

A AssemblyName osztály használata

  1. Hívja meg a AssemblyName.GetAssemblyName metódust, és adja meg a tesztelt fájl teljes elérési útját és nevét.

  2. BadImageFormatException Kivétel esetén a fájl nem szerelvény.

Ez a példa tesztel egy DLL-t, hogy ellenőrizze, hogy szerelvény-e.

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.  

A GetAssemblyName metódus betölti a tesztfájlt, majd az információk beolvasása után felszabadítja azt.

A PEReader osztály használata

  1. Ha a .NET Standardot vagy a .NET-keretrendszer célozza, telepítse a System.ÖnkifejezésIon. Metadata NuGet-csomag. (A .NET Core vagy a .NET 5+ célzása esetén ez a lépés nem szükséges, mert ez a kódtár szerepel a megosztott keretrendszerben.)

  2. Hozzon létre egy példányt System.IO.FileStream , amely adatokat olvas be a tesztelt fájlból.

  3. Hozzon létre egy példányt System.Reflection.PortableExecutable.PEReader , és adja át a fájlstreamet a konstruktornak.

  4. Ellenőrizze a tulajdonság értékét HasMetadata . Ha az érték az false, a fájl nem szerelvény.

  5. GetMetadataReader A PE-olvasó példány metódusának meghívása metaadat-olvasó létrehozásához.

  6. Ellenőrizze a tulajdonság értékét IsAssembly . Ha az érték az true, a fájl egy szerelvény.

A GetAssemblyName metódustól eltérően az PEReader osztály nem kivételt okoz a natív hordozható végrehajtható (PE) fájlokon. Így elkerülheti a kivételek által okozott többletteljesítményt, ha ellenőriznie kell ezeket a fájlokat. Akkor is kezelnie kell a kivételeket, ha a fájl nem létezik, vagy nem PE-fájl.

Ez a példa bemutatja, hogyan állapítható meg, hogy egy fájl szerelvény-e az PEReader osztály használatával.

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.

Lásd még