zoukankan      html  css  js  c++  java
  • C#判断操作系统是32位还是64位(转)

    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。

  • 相关阅读:
    python+selenium截图
    selenium鼠标事件
    python位置参数、默认参数、关键字参数、可变参数的区别
    元素定位
    selenium下拉框选择
    mysql计算日期的函数
    python列表操作
    requests库及请求封装
    什么是接口测试?如何进行接口测试
    类和实例
  • 原文地址:https://www.cnblogs.com/tabcdt/p/3871841.html
Copyright © 2011-2022 走看看