广告
返回顶部
首页 > 资讯 > 后端开发 > 其他教程 >C#判断DLL文件是32位还是64位的示例代码
  • 527
分享到

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

2024-04-02 19:04:59 527人浏览 安东尼
摘要

C#判断dll文件是32位还是64位,实例代码如下所示: using System; using System.io; namespace GetDllVersionDemo

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文件内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!

--结束END--

本文标题: C#判断DLL文件是32位还是64位的示例代码

本文链接: https://www.lsjlt.com/news/160388.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作