C# 获取 Windows 操作系统版本和名称

栏目: ASP.NET · 发布时间: 5年前

内容简介:从 Windows 10 开始,微软已经弃用了 GetVersionEx 方式获取 Windows 系统版本按照微软官方论坛提示,可以通过 WMI(Windows Management Instruction) 获取正确系统的版本号。下面把传统获取方式、WMI获取方式和直接获取操作系统名称的三个方法列出来。传统获取 Windows 系统版本的方法:先获取 Windows 系统的版本号,再通过版本号比对出操作系统名称。

1. 概述

从 Windows 10 开始,微软已经弃用了 GetVersionEx 方式获取 Windows 系统版本 [官方解释] 。这就导致网上一大批C#获取 Windows 版本的代码把 Windows 10 识别为 Windows 8。比如我的电脑 Windows 10 就被识别成 6.2.9200,进而判断出是 Windows 8。

按照微软官方论坛提示,可以通过 WMI(Windows Management Instruction) 获取正确系统的版本号。下面把传统获取方式、WMI获取方式和直接获取操作系统名称的三个方法列出来。

2. 展示

2.1 传统获取方式(弃用)

传统获取 Windows 系统版本的方法:先获取 Windows 系统的版本号,再通过版本号比对出操作系统名称。

public static string get_OSVersion()
{
    //Get Operating system information.
    OperatingSystem os = Environment.OSVersion;
    //Get version information about the os.
    Version vs = os.Version;

    //Variable to hold our return value
    string operatingSystem = "";

    if (os.Platform == PlatformID.Win32Windows)
    {
        //This is a pre-NT version of Windows
        switch (vs.Minor)
        {
            case 0:
                operatingSystem = "95";
                break;
            case 10:
                if (vs.Revision.ToString() == "2222A")
                    operatingSystem = "98SE";
                else
                    operatingSystem = "98";
                break;
            case 90:
                operatingSystem = "Me";
                break;
            default:
                break;
        }
    }
    else if (os.Platform == PlatformID.Win32NT)
    {
        switch (vs.Major)
        {
            case 3:
                operatingSystem = "NT 3.51";
                break;
            case 4:
                operatingSystem = "NT 4.0";
                break;
            case 5:
                if (vs.Minor == 0)
                    operatingSystem = "2000";
                else
                    operatingSystem = "XP";
                break;
            case 6:
                if (vs.Minor == 0)
                    operatingSystem = "Vista";
                else if (vs.Minor == 1)
                    operatingSystem = "7";
                else if (vs.Minor == 2)
                    operatingSystem = "8";
                else
                    operatingSystem = "8.1";
                break;
            case 10:
                operatingSystem = "10";
                break;
            default:
                break;
        }
    }
    //Make sure we actually got something in our OS check
    //We don't want to just return " Service Pack 2" or " 32-bit"
    //That information is useless without the OS version.
    if (operatingSystem != "")
    {
        //Got something.  Let's prepend "Windows" and get more info.
        operatingSystem = "Windows " + operatingSystem;
        //See if there's a service pack installed.
        if (os.ServicePack != "")
        {
            //Append it to the OS name.  i.e. "Windows XP Service Pack 3"
            operatingSystem += " " + os.ServicePack;
        }
        //Append the OS architecture.  i.e. "Windows XP Service Pack 3 32-bit"
        //operatingSystem += " " + getOSArchitecture().ToString() + "-bit";
    }
    //Return the information we've gathered.
    return operatingSystem;
}

3.2 通过 WMI 获取操作系统版本

public static string get_OSVersion()
{
    try
    {
        ManagementObject mo = GetMngObj("Win32_OperatingSystem");

        if (null == mo)
            return string.Empty;

        return mo["Version"] as string;
    }
    catch (Exception e)
    {
        return string.Empty;
    }
}

如下图所示,获取 Windows 10 操作系统版本号正确。

C# 获取 Windows 操作系统版本和名称

3.3 直接获取操作系统名称

根据下面的 Windows 操作系统名称和版本好的对应关系表,可以发现很多不同系列的操作系统拥有相同的主版本号和子版本号,因此通过比对版本号的方式来判断操作系统名称是很不靠谱。

+------------------------------------------------------------------------------------+
|                          |   PlatformID    |   Major version   |   Minor version   |
+------------------------------------------------------------------------------------+
| Windows 95               |  Win32Windows   |         4         |          0        |
| Windows 98               |  Win32Windows   |         4         |         10        |
| Windows Me               |  Win32Windows   |         4         |         90        |
| Windows NT 4.0           |  Win32NT        |         4         |          0        |
| Windows 2000             |  Win32NT        |         5         |          0        |
| Windows XP               |  Win32NT        |         5         |          1        |
| Windows XP 64-Bit Edition|  Win32NT        |         5         |          1        |
| Windows 2003             |  Win32NT        |         5         |          2        |
| Windows Server 2003      |  Win32NT        |         5         |          2        |
| Windows Server 2003 R2   |  Win32NT        |         5         |          2        |
| Windows 2003             |  Win32NT        |         5         |          2        |
| Windows Vista            |  Win32NT        |         6         |          0        |
| Windows 2008             |  Win32NT        |         6         |          0        |
| Windows Server 2008      |  Win32NT        |         6         |          0        |
| Windows 7                |  Win32NT        |         6         |          1        |
| Windows 2008 R2          |  Win32NT        |         6         |          1        |
| Windows Server 2008 R2   |  Win32NT        |         6         |          1        |
| Windows 8                |  Win32NT        |         6         |          2        |
| Windows Server 2012      |  Win32NT        |         6         |          2        |
| Windows 8.1              |  Win32NT        |         6         |          3        |
| Windows Server 2012 R2   |  Win32NT        |         6         |          3        |
+------------------------------------------------------------------------------------+
| Windows Server 2016 Technical Preview|  Win32NT| 10            |          0        |
| Windows 10               |  Win32NT        |        10         |          0        |
+------------------------------------------------------------------------------------+

本文提供了一种简单获取操作系统名称的方式:

new ComputerInfo().OSFullName

需要引用库:using Microsoft.VisualBasic.Devices;

实际测试了 Windows 10 、Windows 2012 R2 和 Windows 2008 R2,如下图所示:

C# 获取 Windows 操作系统版本和名称

C# 获取 Windows 操作系统版本和名称

C# 获取 Windows 操作系统版本和名称

以上,其他版本的操作系统如果获取有问题,还请留言告知,谢谢!

3. 参考内容:

1. windows 10 version 6.2-9200


以上所述就是小编给大家介绍的《C# 获取 Windows 操作系统版本和名称》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

创新者的窘境

创新者的窘境

克莱顿•克里斯坦森( Clayton M. Christensen ) / 胡建桥 / 中信出版社 / 2010-6 / 38.00元

管理类经典图书 o 被《福布斯》评为20世纪最具影响的20本商业图书之一 o “全球商业书籍奖”获奖图书 “颠覆大师”克莱顿•克里斯坦森经典力作。 《金融时报》/布兹•亚兰及汉密顿全球商务书刊颁发“1997年最佳商务书”奖 “1997年最佳商务‘实用’书”奖 一本书, 让志在必得者战战兢兢, 让犹豫不前者胸有成竹, 掀起激荡决策者的脑力风暴, ......一起来看看 《创新者的窘境》 这本书的介绍吧!

JSON 在线解析
JSON 在线解析

在线 JSON 格式化工具

RGB转16进制工具
RGB转16进制工具

RGB HEX 互转工具

UNIX 时间戳转换
UNIX 时间戳转换

UNIX 时间戳转换