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。