溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

C#判斷DLL文件是32位還是64位的示例代碼怎么寫

發(fā)布時(shí)間:2021-12-20 19:04:41 來源:億速云 閱讀:226 作者:柒染 欄目:開發(fā)技術(shù)

C#判斷DLL文件是32位還是64位的示例代碼怎么寫,相信很多沒有經(jīng)驗(yàn)的人對此束手無策,為此本文總結(jié)了問題出現(xiàn)的原因和解決方法,通過這篇文章希望你能解決這個(gè)問題。

c#判斷dll文件是32位還是64位,實(shí)例代碼如下所示:

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("執(zhí)行錯(cuò)誤!");
}

        /// <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位的示例代碼怎么寫

看完上述內(nèi)容,你們掌握C#判斷DLL文件是32位還是64位的示例代碼怎么寫的方法了嗎?如果還想學(xué)到更多技能或想了解更多相關(guān)內(nèi)容,歡迎關(guān)注億速云行業(yè)資訊頻道,感謝各位的閱讀!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

dll
AI