2010年9月30日 星期四

windows 如何判斷64位元環境

由於很多的原因,所以要在windows上面偵測64位元環境的最好方法還是使用WMI比較方便

這邊是一個簡單範例
public static bool DetectIf64bit()
{
bool Is64Bit;
ObjectQuery objQry;
ManagementScope mngScope;
ConnectionOptions connOpt = new ConnectionOptions();
ManagementObjectSearcher searcher;

//-----設定 WMI 連接資訊-----
connOpt.Username = ""; // 登入系統的帳號
connOpt.Password = ""; // 密碼
connOpt.Authentication = AuthenticationLevel.Default; // 連線驗證方式
mngScope = new ManagementScope();
mngScope.Path = new ManagementPath(@"\\.\root\cimv2");
mngScope.Connect();

//----- 透過WMI擷取CPU定址長度資訊
objQry = new ObjectQuery("SELECT * FROM Win32_Processor");
searcher = new ManagementObjectSearcher(mngScope, objQry);
Is64Bit = false;

foreach (ManagementObject QryObj in searcher.Get())
{
if (QryObj.GetPropertyValue("AddressWidth").ToString().Trim() == "64")
{
Is64Bit = true;
break;
}
}

return (Is64Bit);
}

沒有留言: