欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2023-12-29 13:02:16
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文件是32位还是64位的文章就介绍到这了,更多相关c#判断dll文件内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!

相关标签: C# DLL文件

上一篇:

下一篇: