zoukankan      html  css  js  c++  java
  • C# 如何判断系统是64位还是32位【轉】

    判断系统是否是64位的方法有很多。

    对于C#来说,调用WMI是一种简单易行的方式。我们可以用Win32_Processor类里面的AddressWidth属性来表示系统的位宽。AddressWidth的值受CPU和操作系统的双重影响。

    具体的值如下面的表格所示:


    32bit OS64bit OS
    32bit CPUAddressWidth = 32N/A
    64bit CPUAddressWidth = 32AddressWidth = 64


    可以用下面的C#代码得到AddressWidth的值
    (注意需添加引用System.Management)

    public static string Distinguish64or32System()
    {
    try
    {
    string addressWidth = String.Empty;
    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();
    foreach (ManagementObject mObject in mObjectCollection)
    {
    addressWidth
    = mObject["AddressWidth"].ToString();
    }
    return addressWidth;
    }
    catch (Exception ex)
    {
    Console.WriteLine(ex.ToString());
    return String.Empty;
    }
    }

    

    IntPtr.Size, Marshal.SizeOf(typeof(int)) 這兩種方法經測試是錯誤的,運行的時候如果以32位程序去執行就會得到32位結果。

    CPU32位及操作系統64位暫未測試

  • 相关阅读:
    小程序 琐碎
    html + css 琐碎
    mysql 认识
    vue2.x.0 版本新增的 API
    AMD、CMD 和 CommonJS 的区别
    改变UIPageControl圆点间距
    android 内存回收
    ios点击改变uiview背景颜色
    objc_setAssociatedObject 使用
    Position Independent Code (PIC) in shared libraries【转载】
  • 原文地址:https://www.cnblogs.com/ywkpl/p/2059996.html
Copyright © 2011-2022 走看看