zoukankan      html  css  js  c++  java
  • 如何判断系统是64位还是32位

      对于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.dll):

    using System.Managment;
    
    /// <summary>
            /// 检查系统是32位还是64位
            /// </summary>
            /// <returns>0运行出错;32表示32位系统;64表示64位操作系统</returns>
            public static int Distinguish64or32System()
            {
                try
                {
                    //得到AddressWidth值
                    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();
                    string retVal = string.Empty;
                    foreach (ManagementObject mObject in mObjectCollection)
                    {
                        retVal = mObject["AddressWidth"].ToString();
                    }
    
                    //根据值判断,不能直接返回得到的值,
                    //32位返回"32",64位会因为CPU而有两个值,"N/A"或"64"
                    if (retVal == "32")
                    {
                        return 32;
                    }
                    else
                    {
                        return 64;
                    }
                }
                catch
                {
                    return 0;
                }
            }
    

     

  • 相关阅读:
    Codeforces Round #642 (Div. 3)
    [P4980] 【模板】Polya定理
    [SCOI2016] 幸运数字
    [P4389] 付公主的背包
    [CF438E] The Child and Binary Tree
    最长异或路径
    [AHOI2013] 作业
    [P4841] [集训队作业2013] 城市规划
    Python 第三方库国内镜像安装
    [CF1202D] Print a 1337-string...
  • 原文地址:https://www.cnblogs.com/qfcndtt/p/3193253.html
Copyright © 2011-2022 走看看