判斷系統是否是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)的用途還有很多很多,將在我的下一篇文章中詳細的介紹。