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版本描述和版本号

相关推荐
程序设计实验室21 小时前
C# 扩展方法只会写 this 吗?C# 14 新语法直接把扩展方法玩出了花
c#
唐青枫1 天前
C#.NET SignalR 深入解析:实时通信、Hub 与连接管理实战
c#·.net
唐宋元明清21881 天前
.NET Win32磁盘动态卷/跨区卷触发“函数不正确”问题排查
windows·c#·存储
hez20101 天前
Satori GC:同时做到高吞吐、低延时和低内存占用
c#·.net·.net core·gc·clr
唐青枫2 天前
C#.NET Channel 深入解析:高性能异步生产者消费者模型实战
c#·.net
小峥降临3 天前
Rokid UXR 的手势追踪虚拟中更真实的手实战开发【含 工程源码 和 最终完成APK】
c#
晨星shine7 天前
GC、Dispose、Unmanaged Resource 和 Managed Resource
后端·c#
用户298698530147 天前
.NET 文档自动化:Spire.Doc 设置奇偶页页眉/页脚的最佳实践
后端·c#·.net
用户3667462526747 天前
接口文档汇总 - 2.设备状态管理
c#
用户3667462526747 天前
接口文档汇总 - 3.PLC通信管理
c#