How to: Determine If a File Is an Assembly (C# Programming Guide)
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 the topic Assembly Manifest.
How to manually determine if a file is an assembly
Start the MSIL Disassembler (Ildasm.exe).
Load the file you wish to test.
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
Call the GetAssemblyName method, passing the full file path and name of the file you are testing.
If a BadImageFormatException exception is thrown, the file is not an assembly.
Example
This example tests a DLL to see if it is an assembly.
class TestAssembly
{
static void Main()
{
try
{
System.Reflection.AssemblyName testAssembly =
System.Reflection.AssemblyName.GetAssemblyName(@"C:\WINDOWS\system\avicap.dll");
System.Console.WriteLine("Yes, the file is an Assembly.");
}
catch (System.IO.FileNotFoundException e)
{
System.Console.WriteLine("The file cannot be found.");
}
catch (System.BadImageFormatException e)
{
System.Console.WriteLine("The file is not an Assembly.");
}
catch (System.IO.FileLoadException e)
{
System.Console.WriteLine("The Assembly has already been loaded.");
}
}
}
The GetAssemblyName method loads the test file, and then releases it once the information is read.
Output
The file is not an Assembly.
See Also
Tasks
Troubleshooting Exceptions: System.BadImageFormatException
Reference
Concepts
C# Programming Guide
Assemblies and the Global Assembly Cache (C# Programming Guide)