MSDN 如何:确定文件是否为程序集(C#和Visual Basic):
如何以编程方式确定文件是否为程序集
- 调用
GetAssemblyName
方法,传递正在测试的文件的完整文件路径和名称.- 如果
BadImageFormatException
抛出异常,则该文件不是程序集.
它甚至有一个VB.NET示例:
Run Code Online (Sandbox Code Playgroud)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
这并不理想,因为它使用控制流的异常.
我也不确定它在角落情况下的表现如文件是程序集,但不支持当前的CPU体系结构,或者它是否针对框架的不受支持的变体.