C# 如何判斷系統是64位還是32位

來源:互聯網
上載者:User

判斷系統是否是64位的方法有很多。

對於C#來說,調用WMI是一種簡單易行的方式。我們可以用Win32_Processor類裡面的AddressWidth屬性來表示系統的位寬。AddressWidth的值受CPU和作業系統的雙重影響。

具體的值如下面的表格所示:

32bit OS 64bit OS
32bit CPU AddressWidth = 32 N/A
64bit CPU AddressWidth = 32 AddressWidth = 64

可以用下面的C#代碼得到AddressWidth的值
(注意需添加引用System.Management)

public static string Distinguish64or32System()
{
try
{
string addressWidth = String.Empty;
ConnectionOptions mConnOption = new ConnectionOptions();
ManagementScope mMs = new ManagementScope("\\\\localhost", mConnOption);
ObjectQuery mQuery = new ObjectQuery("select AddressWidth from Win32_Processor");
ManagementObjectSearcher mSearcher = new ManagementObjectSearcher(mMs, mQuery);
ManagementObjectCollection mObjectCollection = mSearcher.Get();
foreach (ManagementObject mObject in mObjectCollection)
{
addressWidth = mObject["AddressWidth"].ToString();
}
return addressWidth;
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
return String.Empty;
}
}

關於WMI(Windows Management Instrumentation)的用途還有很多很多,將在我的下一篇文章中詳細的介紹。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.