C#判断DLL文件是32位还是64位的示例代码怎么写

发布时间:2021-12-20 19:04:41 作者:柒染
来源:亿速云 阅读:203

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 = "C#判断DLL文件是32位还是64位";

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文件是32位还是64位的示例代码怎么写的方法了吗?如果还想学到更多技能或想了解更多相关内容,欢迎关注亿速云行业资讯频道,感谢各位的阅读!

推荐阅读:
  1. C#如何调用易语言写的Dll文件
  2. 如何查看python是64位还是32位

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

dll

上一篇:如何用VuePress + Github Pages搭建一个博客

下一篇:基于Python如何进行年龄和性别检测

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》