如果文件存在,我如何使用 VB.Net 进行检查
C:\Documents\Test.exe
是 .net 二进制文件还是 native 二进制文件?
最佳答案
MSDN How to: Determine If a File Is an Assembly (C# and Visual Basic) :
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.
它甚至还有一个 VB.NET 示例:
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 架构,或者它是否针对不受支持的框架变体。
关于c# - VB.Net - 检查文件是否为 .net 二进制文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22237126/