获取系统版本
每一种方式都能获取系统版本,但是对于Win11,有的方式获取出来的依旧是Win10。
通过版本号的方式可以通过构建号来判断Win11,需要添加
应用程序清单文件(仅限Windows)。通过注册表获取不能判断Win11。
通过InteropServices不能判断Win11。
通过Management可以判断Win11。
通过Version获取
获取版本名称
注意:
通过版本号不能判断Win11,但是这里可以通过构建号来判断是否为Win11。
需要添加
应用程序清单文件(仅限Windows)。
代码
1  | /// <summary>  | 
比较版本
需要添加应用程序清单文件(仅限Windows)。
判断是否是Win10及以上:
1  | using System;  | 
Win10获取的是Win8
此方法在Win10下获取的值可能不是10,这是因为版本不兼容,解决方案是程序应用清单中增加配置:
添加 => 新建项 => 应用程序清单文件(仅限Windows)
 
添加后属性中已经默认选择了这个清单文件
 
把清单中的这些配置解除注释
1  | <compatibility xmlns="urn:schemas-microsoft-com:compatibility.v1">  | 
这时候就能正常获取Win10了。
版本号
| Version | 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 | 
| Windows 11 | Win32NT | 10 | 0 | 
通过注册表获取
这种方式Win10及以前都是准确的,但是
这种方式Win11会获取为Win10。
代码
1  | /// <summary>  | 
通过InteropServices获取
注意:
这种方式Win11会获取为Win10。
添加引用
1  | System.Runtime.InteropServices.Runtimelnformation  | 
判断是否是Windows
1  | RuntimeInformation.IsOSPlatform(OSPlatform.Windows)  | 
获取系统版本
1  | public static string GetSysVersion()  | 
Management方式(推荐)
注意
这种方式能获取到Win11。
添加引用
1  | System.Management  | 
代码
1  | /// <summary>  | 
系统位数
1  | /// <summary>  | 
品牌型号
1  | /// <summary>  | 
其他可获取的值
常用的值
| 类型 | 类 | Key | 
|---|---|---|
| 系统版本 | Win32_OperatingSystem | Caption | 
| 电脑型号 | Win32_ComputerSystem | Model | 
| 电脑品牌 | Win32_ComputerSystem | Manufacturer | 
| 工作组 | Win32_ComputerSystem | Domain | 
| 登录用户 | Win32_ComputerSystem | UserName | 
| 计算机名 | Win32_ComputerSystem | Name | 
| 最后盘符 | Win32_BootConfiguration | LastDrive | 
| 主板序列号 | Win32_ComputerSystemProduct | IdentifyingNumber | 
测试结果
1  | private void GetSysInfo()  | 
结果如下:
1  | InteropServices方式:Windows 10.0.22621  |