c# - VB.Net - 检查文件是否为 .net 二进制文件

标签 c# vb.net file native managed

如果文件存在,我如何使用 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

  1. Call the 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.

它甚至还有一个 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/

相关文章:

c# - 构建具有多重排序的表达式

vb.net - 显示两个 RichTextBox 控件之间的区别

c - libev 网络服务器读取文件

.net - vb.net - 为什么 ULong (UInt64) 不允许 += 运算符?

Python:具有程序员定义属性的文件

python - 从python中的.txt文件中读取特殊字符

c# - 将 varbinary 转换为图像

c# - TypeLoadException - 如何继续

c# - 当 DataGridView 具有焦点时,如何让 "Accept"按钮触发?

c# - 单步执行 .NET 类包装的 VB6 COM 对象