# How to: Determine if a file is an assembly

A file is an assembly if and only if it is managed, and contains an assembly entry in its metadata. For more information on assemblies and metadata, see Assembly manifest.

## How to manually determine if a file is an assembly

1. Start the Ildasm.exe (IL Disassembler).

2. Load the file you want to test.

3. If ILDASM reports that the file is not a portable executable (PE) file, then it is not an assembly. For more information, see the topic How to: View assembly contents.

## How to programmatically determine if a file is an assembly

### Using the AssemblyName class

1. Call the AssemblyName.GetAssemblyName method, passing the full file path and name of the file you are testing.

2. If a BadImageFormatException exception is thrown, the file is not an assembly.

This example tests a DLL to see if it is an assembly.

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.");
}
{
Console.WriteLine("The file is not an assembly.");
}
{
}
}

/* 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.")
Console.WriteLine("The file is not an Assembly.")
End Try
End Sub
End Module
' Output:
' Yes, the file is an Assembly.


The GetAssemblyName method loads the test file, and then releases it once the information is read.

1. Install the System.Reflection.Metadata NuGet package.

2. Create a System.IO.FileStream instance to read data from the file you're testing.

3. Create a System.Reflection.PortableExecutable.PEReader instance, passing your file stream into the constructor.

4. Check the value of the HasMetadata property. If the value is false, the file is not an assembly.

6. Check the value of the IsAssembly property. If the value is true, the file is an assembly.

Unlike the GetAssemblyName method, the PEReader class does not throw an exception on native Portable Executable (PE) files. This enables you to avoid the extra performance cost caused by exceptions when you need to check such files. You still need to handle exceptions in case the file does not exist or is not a PE file.

This example shows how to determine if a file is an assembly using the PEReader class.

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

{
static bool IsAssembly(string path)
{

{
return false; // File does not have CLI metadata.
}

// Check that file has an assembly manifest.
}

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.");
}
}
{
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.PortableExecutable
Imports System.Runtime.InteropServices

Function IsAssembly(path As String) As Boolean

Return False ' File does Not have CLI metadata.
End If

' Check that file has an assembly manifest.
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