判断操作系统的位数有一下几种方法:

1. 特征值IntPtr

2. WMI

1的实现如下:

public static int GetOSInfo()
{
if (IntPtr.Size == 8)
{
return 64;
}
else
{
return 32;
}
}

但是有问题,如果应用运行的是x86 的模式,判断就会有误,如何解决?

添加一下代码:

public static bool Is64BitWindows {
get {
// this is a 64-bit process -> Windows is 64-bit
if (IntPtr.Size == 8)
return true;

// this is a 32-bit process -> we need to check whether we run in Wow64 emulation
bool is64Bit;
if (IsWow64Process(GetCurrentProcess(), out is64Bit)) {
return is64Bit;
} else {
throw new Win32Exception(Marshal.GetLastWin32Error());
}
}
}

[DllImport("kernel32", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
public  static extern bool IsWow64Process(IntPtr hProcess, out bool wow64Process);

[DllImport("kernel32")]
public  static extern IntPtr GetCurrentProcess();

即可,这样做可以保证是正确的。

2的实现方法如下:

public static int GetOSBit()
{
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 Int32.Parse(addressWidth);
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
return 32;
}
}
}

以上为两种实现方法。

C# 判断操作系统的位数的更多相关文章

  1. [转]CPU的位数与操作系统的位数的区别

    转自:http://weiheyouchou.blog.hexun.com/35564976_d.html 随着近来AMD和Intel的64位CPU以及 Microsoft 64位操作系统的相继发布, ...

  2. 如何查看Linux操作系统的位数

    如何查看Linux操作系统的位数 1.编程实现: 在程序中返回sizeof(int)的值,返回的结果是操作系统的字节数.若返回4则是32位操作系统,返回8即是64位. 2.2.getconf命令: g ...

  3. Linux下查看操作系统的位数和系统名称版本信息

    Linux下如何明确地查看操作系统的位数 如何知晓操作系统是32位还是64位?这里介绍一种简单的方式: [plain] [root@localhost mysql-5.1.57]# getconf L ...

  4. Ubuntu 查看操作系统的位数

    查看Ubuntu操作系统的位数是32位还是64位,可以通过以下命令来查看: getconf LONG_BIT 返回32或64 :如图

  5. bat 获取管理员权限,判断系统位数,获取当前文件所在目录,regsvr32注册DLL、OCX

    1.获取管理员权限 @echo off if exist "%SystemRoot%\SysWOW64" path %path%;%windir%\SysNative;%Syste ...

  6. 利用Javascript判断操作系统的类型实现不同操作系统下的兼容性

    原文地址 http://www.jb51.net/article/33640.htm 在通过Javascript实现客户端和服务端的交互时,有时候需要对操作系统进行判断,以便实现不同操作系统下的兼容性 ...

  7. SQL Server判断小数位数

    项目中需要写一规则,目的是判断数值的小数位数,可以分为2中情况. 1.直接以小数点为分界点,小数点后的数据表示小数的位数,此种情况比较简单,直接使用CHARINDEX函数就可以搞定 其中CHARIND ...

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

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

  9. [转]C#如何判断操作系统位数是32位还是64位

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

随机推荐

  1. poj1981 Circle and Points

    地址:http://poj.org/problem?id=1981 题目: Circle and Points Time Limit: 5000MS   Memory Limit: 30000K To ...

  2. Union、Union All、Intersect、Minus用法和区别

    假设我们有一个表Student,包括以下字段与数据: [c-sharp] view plain copydrop table student;    create table student  (   ...

  3. Makefile解析(最简单的LED)

    ①led_sp.bin: start.o led.o #led_sp.bin是由 start.o 和 led.o 生成 ②arm-linux-ld -Ttext 0x0 -o led_sp.elf $ ...

  4. Python学习札记(四十三) IO 3

    参考:操作文件和目录 NOTE: 1.Python内置的os模块可以直接调用操作系统提供的接口函数: 2.os.name 打印操作系统的名称:如果是posix,说明系统是Linux.Unix或Mac ...

  5. python 计算阶乘

    # 用for循环计算 n! sum = n=int(input('请输入n=')) ,n+): ,-): sum *= j # sum=sum*j print('%d!=%3d' %(i,sum)) ...

  6. Flutter基础Widget之按钮(RaisedButton、FlatButton、OutlineButton,IconButton)

    Flutter中给我们预先定义好了一些按钮控件给我们用,常用的按钮如下 RaisedButton :凸起的按钮,其实就是Android中的Material Design风格的Button ,继承自Ma ...

  7. 设计点滴&css效果点滴

    走向设计师的第一步, 做一个自由的设计师. 优秀的移动端设计的:http://www.cnblogs.com/coding4/p/6842849.html 一些好的设计图片的收藏,一些好的设计理念,一 ...

  8. android-------非常好的图片加载框架和缓存库(Picasso)

    Picasso是Square公司开源的一个Android图形缓存库, 可以实现图片加载(本地和网络)和缓存功能. 地址:http://square.github.io/picasso/ jar包下载: ...

  9. linux进程概论

    1操作系统几大模块 进程管理,进程调度,进程间通讯机制,内存管理,中断异常处理,文件系统,I/O系统,网网络部分. 2操作系统的目的 管理硬件设备,为上层应用程序提供良好的执行环境. 3linux系统 ...

  10. wikioi 1021 玛丽卡

    链接:http://wikioi.com/problem/1021/ 这题挺有意思的,虽然比较水,但是让我想起来那次百度or腾讯的一道最大流的题目,很给力,也是对最后找边进行优化,不过这题比那题简单多 ...