C# 获取Windows系统版本注意事项

首先通过微软官方文档:https://learn.microsoft.com/zh-cn/windows/win32/sysinfo/operating-system-version了解各个操作系统对应的版本号

下面介绍3种获取版本号的方式及弊端

  1. Environment.OSVersion.Version

    复制代码
             OperatingSystem os = Environment.OSVersion;
    
             // 判断操作系统版本
             if (os.Version.Major == 10 && os.Version.Build < 22000)
             {
                 textBlock.Text = "当前操作系统是 Windows 10";
             }
             else if (os.Version.Major == 10 && os.Version.Build >= 22000)
             {
                 textBlock.Text = "当前操作系统是 Windows 11";
             }
             else
             {
                 textBlock.Text = $"当前操作系统既不是  Windows 10也不是 Windows 11";
             }

该方式适合.NET Framework,并且必须添加应用程序清单文件,并且需要把程序应用程序清单文件里面的supportedOS项取消注释(默认是注释的)

  1. System.Runtime.InteropServices.RuntimeInformation.OSDescription

    复制代码
             var osDescription = System.Runtime.InteropServices.RuntimeInformation.OSDescription;
             var osDescs = osDescription.Split(' ');
             foreach (var osDesc in osDescs)
             {
                 if (!osDesc.Contains(".")) continue;
                 var versions = osDesc.Split('.');
                 if (versions.Length < 2) continue;
                 if (int.Parse(versions[0]) == 10 && int.Parse(versions[2]) < 22000)
                 {
                     textBlock.Text = "当前操作系统是 Windows 10";
                 }
                 else if (int.Parse(versions[0]) == 10 && int.Parse(versions[2]) >= 22000)
                 {
                     textBlock.Text = "当前操作系统是 Windows 11";
                 }
                 else
                 {
                     textBlock.Text = $"当前操作系统既不是  Windows 10也不是 Windows 11";
                 }
             }

该方式适合.NET Core以上(.net framework 4.7以上)

  1. 从注册表中获取版本信息

    复制代码
             using (RegistryKey key = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Windows NT\CurrentVersion"))
             {
                 string productName = key.GetValue("ProductName") as string;
                 int majorVersion = (int)key.GetValue("CurrentMajorVersionNumber");
                 var buildNumber = int.Parse(key.GetValue("CurrentBuildNumber").ToString());
    
                 if (!string.IsNullOrEmpty(productName) && productName.ToLower().Contains("windows"))
                 {
                     if (majorVersion > 10 || majorVersion == 10 && buildNumber >= 22000)
                     {
                         textBlock.Text = "当前操作系统是 Windows 11";
                     }
                     else if (majorVersion == 10 && buildNumber < 22000)
                     {
                         textBlock.Text = "当前操作系统是 Windows 10";
                     }
                     else
                     {
                         textBlock.Text = $"当前操作系统既不是  Windows 10也不是 Windows 11";
                     }
                 }
             }

该方式暂时没发现问题,但是需要使用版本号推断是Win11

4. 从WMI获取系统版本(推荐)

复制代码
            foreach (var o in new ManagementObjectSearcher("SELECT * FROM Win32_OperatingSystem").Get())
            {
                var obj = (ManagementObject)o;

                // Microsoft Windows 11 企业版
                var caption = obj.GetPropertyValue("Caption");

                // 10.0.22000
                var version = obj.GetPropertyValue("Version");
                break;
            }

该方式可以准确获取Windows版本描述和版本号

相关推荐
Scout-leaf2 天前
WPF新手村教程(三)—— 路由事件
c#·wpf
用户298698530142 天前
程序员效率工具:Spire.Doc如何助你一键搞定Word表格排版
后端·c#·.net
埃博拉酱3 天前
VS Code Remote SSH 连接 Windows 服务器卡在"下载 VS Code 服务器":prcdn DNS 解析失败的诊断与 BITS 断点续传
windows·ssh·visual studio code
mudtools3 天前
搭建一套.net下能落地的飞书考勤系统
后端·c#·.net
玩泥巴的4 天前
搭建一套.net下能落地的飞书考勤系统
c#·.net·二次开发·飞书
唐宋元明清21884 天前
.NET 本地Db数据库-技术方案选型
windows·c#
郑州光合科技余经理4 天前
代码展示:PHP搭建海外版外卖系统源码解析
java·开发语言·前端·后端·系统架构·uni-app·php
lindexi4 天前
dotnet DirectX 通过可等待交换链降低输入渲染延迟
c#·directx·d2d·direct2d·vortice
feifeigo1234 天前
matlab画图工具
开发语言·matlab
加号34 天前
windows系统下mysql多源数据库同步部署
数据库·windows·mysql