有些时候我们需要判断一下dll文件是32位还是64位,纠结该如何操作呢,下面小编通过实例代码给大家介绍下C#判断DLL文件是32位还是64位,感兴趣的朋友跟随小编一起看看吧
c#判断dll文件是32位还是64位,实例代码如下所示:
using System;
using System.IO;
namespace GetDllVersionDemo
{
/// <summary>
/// https://www.cnblogs.com/LifeDecidesHappiness/p/15711169.html
/// C#判断DLL文件是32位还是64位
/// LDH @ 2021-12-20
/// </summary>
internal class Program
{
private static void Main()
{
Console.Title = "QyPliKTmlq1ETEzmlofku7bmmK8zMuS9jei/mOaYrzY05L2N";
GetDll32Or64();
Console.ReadKey();
}
private static void GetDll32Or64()
{
var dllPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, @"Dll\IBM.Data.Informix.dll");
var result = GetPeArchitecture(dllPath);
//523 64位 267 32位
if (result == 523)
Console.WriteLine(dllPath + "是【64】位的dll");
else if (result == 267)
Console.WriteLine(dllPath + "是【32】位的dll");
else
Console.WriteLine("执行错误!");
}
/// <summary>
/// 获取dll文件是32位还是64位
/// 523 64位 267 32位
/// </summary>
/// <param name="dllFilePath">dll文件路径</param>
/// <returns></returns>
public static ushort GetPeArchitecture(string dllFilePath)
{
ushort architecture = 0;
try
{
using (var fStream = new FileStream(dllFilePath, FileMode.Open, FileAccess.Read))
{
using (var bReader = new BinaryReader(fStream))
{
if (bReader.ReadUInt16() == 23117) //check the MZ signature
{
fStream.Seek(0x3A, SeekOrigin.Current); //seek to e_lfanew.
fStream.Seek(bReader.ReadUInt32(), SeekOrigin.Begin); //seek to the start of the NT header.
if (bReader.ReadUInt32() == 17744) //check the PE\0\0 signature.
{
fStream.Seek(20, SeekOrigin.Current); //seek past the file header,
architecture = bReader.ReadUInt16(); //read the magic number of the optional header.
}
}
}
}
}
catch
{
// ignored
}
// if architecture returns 0, there has been an error.
return architecture;
}
}
}
到此这篇关于C#判断DLL文件是32位还是64位的文章就介绍到这了,更多相关C#判断DLL文件内容请搜索得得之家以前的文章希望大家以后多多支持得得之家!
沃梦达教程
本文标题为:C#判断DLL文件是32位还是64位的示例代码
![](/xwassets/images/pre.png)
![](/xwassets/images/next.png)
猜你喜欢
- Oracle中for循环的使用方法 2023-07-04
- WPF使用DrawingContext实现绘制刻度条 2023-07-04
- 如何使用C# 捕获进程输出 2023-03-10
- user32.dll 函数说明小结 2022-12-26
- .NET CORE DI 依赖注入 2023-09-27
- Unity3D实现渐变颜色效果 2023-01-16
- 在C# 8中如何使用默认接口方法详解 2023-03-29
- c# 模拟线性回归的示例 2023-03-14
- Unity Shader实现模糊效果 2023-04-27
- C# 使用Aspose.Cells 导出Excel的步骤及问题记录 2023-05-16