方法: ファイルがアセンブリであるかどうかを確認する (C# および Visual Basic)

ファイルが管理されていて、そのメタデータにアセンブリ エントリが含まれている場合に限り、そのファイルはアセンブリです。 アセンブリとメタデータの詳細については、「アセンブリ マニフェスト」を参照してください。

ファイルがアセンブリであるかどうかを手動で確認するには

  1. Ildasm.exe (MSIL 逆アセンブラー) を起動します。

  2. テストするファイルを読み込みます。

  3. ILDASM で、そのファイルがポータブル実行可能 (PE) ファイルではないと報告された場合、そのファイルはアセンブリでありません。 詳細については、「方法 : アセンブリの内容を表示する」を参照してください。

ファイルがアセンブリであるかどうかをプログラムによって確認するには

  1. GetAssemblyName メソッドを呼び出し、テストするファイルの完全パスと名前を渡します。

  2. BadImageFormatException 例外がスローされた場合、ファイルはアセンブリでありません。

使用例

次の例では、DLL がアセンブリであるかどうかをテストして確認します。

Module Module1
    Sub Main()
        Try
            Dim testAssembly As Reflection.AssemblyName =
                                Reflection.AssemblyName.GetAssemblyName("C:\Windows\Microsoft.NET\Framework\v3.5\System.Net.dll")
            Console.WriteLine("Yes, the file is an Assembly.")
        Catch ex As System.IO.FileNotFoundException
            Console.WriteLine("The file cannot be found.")
        Catch ex As System.BadImageFormatException
            Console.WriteLine("The file is not an Assembly.")
        Catch ex As System.IO.FileLoadException
            Console.WriteLine("The Assembly has already been loaded.")
        End Try
        Console.ReadLine()
    End Sub
End Module
' Output (with .NET Framework 3.5 installed):
'        Yes, the file is an Assembly.
class TestAssembly
{
    static void Main()
    {

        try
        {
            System.Reflection.AssemblyName testAssembly =
                System.Reflection.AssemblyName.GetAssemblyName(@"C:\Windows\Microsoft.NET\Framework\v3.5\System.Net.dll");

            System.Console.WriteLine("Yes, the file is an assembly.");
        }

        catch (System.IO.FileNotFoundException)
        {
            System.Console.WriteLine("The file cannot be found.");
        }

        catch (System.BadImageFormatException)
        {
            System.Console.WriteLine("The file is not an assembly.");
        }

        catch (System.IO.FileLoadException)
        {
            System.Console.WriteLine("The assembly has already been loaded.");
        }
    }
}
/* Output (with .NET Framework 3.5 installed):
    Yes, the file is an assembly.
*/

GetAssemblyName メソッドはテスト ファイルを読み込み、情報が読み取られた時点で解放します。

参照

参照

アセンブリとグローバル アセンブリ キャッシュ (C# および Visual Basic)

AssemblyName

概念

C# プログラミング ガイド

その他の技術情報

Visual Basic のプログラミング ガイド