This is an interesting issue because it touches one of the .NET essentials, PE format. So even though I talked about it in this post earlier, I think another post is worthwhile.
Yes, using the sample code in last post I was able to determine if a file is .NET assembly but it was not efficient. Because of a well known .NET bug that assemblies loaded by reflection cannot be unloaded easily. Thus, if you try to verify a thousand files, then you may waste a lot of memory unexpectedly.
It is quite lucky that today NDepend author Patrick Smacchia talked about a similar topic here. Suddenly I realize that I should drop System.Reflection and try Mono.Cecil instead.
http://codebetter.com/blogs/patricksmacchia/archive/2008/03/18/mono-cecil-vs-system-reflection.aspx
So right now I use this sample code in 6.0 Update 1 RC,
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
public static bool IsDotNetAssembly(string fileName)
{
bool result = true;
Mono.Cecil.AssemblyDefinition myLibrary = null;
try
{
myLibrary = Mono.Cecil.AssemblyFactory.GetAssembly (fileName);
}
catch (Mono.Cecil.Binary.ImageFormatException)
{
// for win32 dll
result = false;
}
catch (ArgumentOutOfRangeException)
{
// for win32 exe
result = false;
}
return result && myLibrary != null;
}