標籤:style os io for cti 代碼 div ar
1 根據控制代碼長度判斷作業系統是否為64位作業系統public static bool IsRunningOn64Bit { get { return IntPtr.Size == 8; } }2 根據控制代碼長度判斷作業系統是否為64位作業系統(不安全的程式碼)public static unsafe bool IsRunningOn64Bit { get { return (sizeof(IntPtr) == sizeof(long)); } }
將項目做如下設定:項目屬性對話方塊->配置屬性->產生->允許不安全的程式碼塊 設為"true"
3 根據AddressWidth屬性判斷
AddressWidth的值受CPU和作業系統的雙重影響。如下:
|
32bit OS |
64bit OS |
32bit CPU |
AddressWidth = 32 |
N/A |
64bit CPU |
AddressWidth = 32 |
AddressWidth = 64 |
以下程式段讀取AddressWidth的值
public static string GetAddressWidth() { ConnectionOptions oConn = new ConnectionOptions(); System.Management.ManagementScope oMs = new System.Management.ManagementScope("\\\\localhost", oConn); System.Management.ObjectQuery oQuery = new System.Management.ObjectQuery("select AddressWidth from Win32_Processor"); ManagementObjectSearcher oSearcher = new ManagementObjectSearcher(oMs, oQuery); ManagementObjectCollection oReturnCollection = oSearcher.Get(); string addressWidth = null; foreach (ManagementObject oReturn in oReturnCollection) { addressWidth = oReturn["AddressWidth"].ToString(); } return addressWidth; }4 判斷作業系統是否為64位作業系統bool IsRunningOn64Bit() { #if defined(_WIN64) return true; // 64-bit programs run only on Win64 #elif defined(_WIN32) // 32-bit programs run on both 32-bit and 64-bit Windows // so must sniff bool f64 = false; return IsWow64Process(GetCurrentProcess(), &f64) && f64; #else return false; // Win64 does not support Win16 #endif }
網友評論:不要用IsWow64Process(),Wow64特指Windows32 On Win64。