C#如何判断操作系统位数是32位还是64位

简介:

方法一:
对于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)

复制代码
public static string Detect3264()
{
             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;
}
复制代码

方法二:(需Framework4.0+)

     bool type;
     type = Environment.Is64BitOperatingSystem;
     Console.WriteLine(type);

如返回值为True则表示是64位,如返回值为False则表示为32位。

方法三:
这个方法也是最直接的方法,但是有条件限制,例用IntPtr结构的size属性来查看系统的位宽
命名空间是System;
前题是程序需要采用any/CPU的方式进行编辑;
正常情况下int的位宽是4位,即是32位操作系统。

复制代码
if (IntPtr.Size == 8)
{ 
  //64 bit
}
else if (IntPtr.Size == 4)
{ 
  //32 bit
}
else 
{
  //...NotSupport
}
复制代码

方法四:
64位Wnidows 里面有个叫Wow64 的模拟器技术,可以使32位的程序在64位Windows 上运行。 当你想在程序里面针对32b位/ 64位系统执行不同代码的时候, 需要判断操作系统是32位还是64位。 使用 Windows API函数 GetNativeSystemInfo 可以获得这个信息。

复制代码
SYSTEM_INFO si; 
GetNativeSystemInfo(&si); 
if (si.wProcessorArchitecture == PROCESSOR_ARCHITECTURE_AMD64 ||    
si.wProcessorArchitecture != PROCESSOR_ARCHITECTURE_IA64 ) 
{ 
//64 位操作系统 
} 
else 
{ 
// 32 位操作系统 
}
复制代码

另外,Windows API 还提供了 IsWow64Process 函数判断程序是不是运行在Wow64模拟器之上。

BOOL bIsWow64 = FALSE; 
IsWow64Process(GetCurrentProcess(), &bIsWow64);

需要注意是GetNativeSystemInfo  函数从Windows XP 开始才有, 而 IsWow64Process  函数从 Windows XP with SP2 以及 Windows Server 2003 with SP1 开始才有。 所以使用该函数的时候最好用GetProcAddress。

复制代码
typedef void (WINAPI *LPFN_PGNSI)(LPSYSTEM_INFO); 

typedef BOOL (WINAPI *LPFN_ISWOW64PROCESS) (HANDLE, PBOOL);     

LPFN_PGNSI pGNSI = (LPFN_PGNSI ) GetProcAddress(    
             GetModuleHandle(TEXT("kernel32.dll")),    "GetNativeSystemInfo"); 

LPFN_ISWOW64PROCESS    fnIsWow64Process = (LPFN_ISWOW64PROCESS) GetProcAddress( 
             GetModuleHandle(TEXT("kernel32")),"IsWow64Process"); 
复制代码

摘自:http://www.cnblogs.com/czzju/articles/2482474.html


目录
相关文章
|
1月前
|
C#
C#中小数保留固定位数
C#中小数保留固定位数
19 1
C#编程-24:String前面不足位数补零的方法
C#编程-24:String前面不足位数补零的方法
|
Windows
.NET获取操作系统版本、获取操作系统位数(转载)
添加引用System.Management using System; using System.Collections.Generic; using System.
832 0
|
分布式计算 Hadoop Linux
部署Hadoop,需和Linux操作系统位数一致
本次实验的环境:linux 64位+hadoop2.7.2 64位. HADOOP_HOME=/hadoop/hadoop-2.7 点击(此处)折叠或打开 [root@sht-sgmhadoopnn-...
920 0
C# String 前面不足位数补零的方法
在 C# 中可以对字符串使用 PadLeft 和 PadRight 进行轻松地补位。 PadLeft(int totalWidth, char paddingChar) //在字符串左边用 paddingChar 补足 totalWidth 长度 PadLeft(int totalWidth, char paddingChar) //在字符串右边用 paddingChar 补足
1955 0
|
编译器 Linux C语言
CPU位数、操作系统位数和编译器位数关系
<p><strong style="line-height: 1.5;"><span style="font-size: 18pt;"><span style="color: #ff0000;">编译器</span>是将一种语言翻译为另一种语言的计算机程序。编译器将源程序(source language) 编写的程序作为输入,而产生用目标语言(target language )编写的等价程序。
2503 0