码迷,mamicode.com
首页 > 其他好文 > 详细

C#判断操作系统是32位还是64位(转)

时间:2014-07-27 23:10:39      阅读:226      评论:0      收藏:0      [点我收藏+]

标签: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。

C#判断操作系统是32位还是64位(转),布布扣,bubuko.com

C#判断操作系统是32位还是64位(转)

标签:style   os   io   for   cti   代码   div   ar   

原文地址:http://www.cnblogs.com/tabcdt/p/3871841.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!