C#获取操作系统版本号方法

在 C# 中,可以通过 Environment.OSVersionWMI (Windows Management Instrumentation) 获取 Windows 10 或 Windows 11 的版本信息。但由于 Windows 11 修改了版本号格式(仍是 10.0 ,单独使用 OSVersion 可能无法区分 Win10 和 Win11,需要额外的方法来判断。


📌 4 种方法检测 Windows 10/11

方法 优点 缺点
Environment.OSVersion 简单快速 Win10/Win11 都返回 10.0
Registry 直接读取内部版本号(22000+) 需要访问注册表
WMI (Win32_OS) 官方推荐方式 查询较慢
IsWindows11OrGreater() (WinAPI) 微软官方方法(推荐) 仅支持 .NET 5+

1️⃣ Environment.OSVersion(基础判断,无法区分 Win10/Win11)

csharp 复制代码
var os = Environment.OSVersion;
Console.WriteLine($"平台: {os.Platform}");         // Win32NT
Console.WriteLine($"主版本: {os.Version.Major}");  // 10(Win10/Win11 均为 10)
Console.WriteLine($"次版本: {os.Version.Minor}");  // 0(Win10/Win11 均为 0)
Console.WriteLine($"Build: {os.Version.Build}");    // 19044(Win10) / 22000(Win11)

局限性

  • Windows 11 仍返回 10.0(兼容性问题)
  • 只能靠 Build 版本区分:
  • Windows 10 : < 22000
  • Windows 11 : ≥ 22000(首个正式版是 22000

2️⃣ 读取注册表(Get Build Number)

Win11 的内部版本号 ≥ 22000

csharp 复制代码
using Microsoft.Win32;

int GetWinBuildNumber()
{
    using (RegistryKey key = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Windows NT\CurrentVersion"))
    {
        return int.Parse(key.GetValue("CurrentBuildNumber").ToString());
    }
}

var build = GetWinBuildNumber();
if (build >= 22000)
    Console.WriteLine("Windows 11");
else
    Console.WriteLine("Windows 10");

3️⃣ WMI(通过 Win32_OperatingSystem 查询)

csharp 复制代码
using System.Management;

string GetOSName()
{
    var searcher = new ManagementObjectSearcher("SELECT Caption, BuildNumber FROM Win32_OperatingSystem");
    foreach (ManagementObject os in searcher.Get())
    {
        string caption = os["Caption"].ToString();
        int build = int.Parse(os["BuildNumber"].ToString());
        
        if (build >= 22000)
            return "Windows 11";
        else
            return "Windows 10";
    }
    return "Unknown";
}

Console.WriteLine(GetOSName());

📌 注意

  1. 需要 System.Management NuGet 包:
bash 复制代码
   Install-Package System.Management
  1. WMI 查询较慢,但可以获取更多 OS 信息(如系统名称、位数)。

4️⃣ 使用 WinAPI IsWindows11OrGreater()(推荐 .NET 5+)

如果你使用的是 .NET 5 / 6 / 7,可以直接调用 Windows API:

csharp 复制代码
using System.Runtime.InteropServices;

public static class WinAPI
{
 [DllImport("kernel32.dll")]
 private static extern ulong VerifyVersionInfoW(
 ref OSVERSIONINFOEX lpVersionInfo,
 uint dwTypeMask,
 ulong dwlConditionMask
 );

 [DllImport("kernel32.dll")]
 private static extern ulong VerSetConditionMask(
 ulong dwlConditionMask,
 uint dwTypeBit,
 uint dwConditionMask
 );

 [StructLayout(LayoutKind.Sequential)]
 private struct OSVERSIONINFOEX
 {
 public uint dwOSVersionInfoSize;
 public uint dwMajorVersion;
 public uint dwMinorVersion;
 public uint dwBuildNumber;
 public uint dwPlatformId;
 [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 128)]
 public string szCSDVersion;
 public ushort wServicePackMajor;
 public ushort wServicePackMinor;
 public ushort wSuiteMask;
 public byte wProductType;
 public byte wReserved;
 }

 public static bool IsWindows11OrGreater()
 {
 const uint VER_MAJORVERSION = 0x0000002;
 const uint VER_MINORVERSION = 0x0000001;
 const uint VER_BUILDNUMBER = 0x0000004;
 const uint VER_GREATER_EQUAL = 0x3;

 ulong conditionMask = 0;
 conditionMask = VerSetConditionMask(conditionMask, VER_MAJORVERSION, VER_GREATER_EQUAL);
 conditionMask = VerSetConditionMask(conditionMask, VER_MINORverbsion, VER_GREATER_EQUAL);
 conditionMask = VerSetConditionMask(conditionMask, VER_BUILDNUMBER, VER_GREATER_EQUAL);

 OSVERSIONINFOEX versionInfo = new OSVERSIONINFOEX();
 versionInfo.dwOSVersionInfoSize = (uint)Marshal.SizeOf(versionInfo);
 versionInfo.dwMajorVersion = 10;
 versionInfo.dwMinorVersion = 0;
 versionInfo.dwBuildNumber = 22000; // Windows 11 ≥ 22000

 return VerifyVersionInfoW(ref versionInfo, VER_MAJORVERSION | VER_MINORVERSION | VER_BUILDNUMBER, conditionMask) != 0;
 }
}

// 调用
bool isWin11 = WinAPI.IsWindows11OrGreater();
Console.WriteLine(isWin11 ? "Windows 11" : "Windows 10");

优点

  • 微软官方推荐方法
  • 精确判断 Win11
    适用 .NET 5 / 6 / 7

📌 最终推荐方案

场景 推荐方法
简单判断(仅需 Build) Registry / OSVersion.Build
精确判断(兼容 Win11) IsWindows11OrGreater()(WinAPI)
多 OS 信息(名称、版本) WMI (Win32_OS)

👉 最优解

如果你的应用 仅针对 Windows 10/11推荐直接用注册表或 Build 判断

csharp 复制代码
int build = GetWinBuildNumber(); // 方法2
Console.WriteLine(build >= 22000 ? "Windows 11" : "Windows 10");

更简单,无需复杂 API 调用!


🚀 额外(检测 Win10/11 版本号映射)

Windows 版本 Build 号
Windows 10 1809 17763
Windows 10 1909 18363
Windows 10 2004 19041
Windows 10 21H2 19044
Windows 11 21H2 22000
Windows 11 22H2 22621

所有 Win11 Build ≥ 22000

相关推荐
β添砖java14 小时前
python第一阶段第10章
开发语言·python
倔强的小石头_15 小时前
Python 从入门到实战(八):类(面向对象的 “对象模板”)
服务器·开发语言·python
Mr_Xuhhh15 小时前
第一部分:类和对象(中)— 取地址运算符重载
java·开发语言
Selegant15 小时前
告别传统部署:用 GraalVM Native Image 构建秒级启动的 Java 微服务
java·开发语言·微服务·云原生·架构
helloworddm15 小时前
LocalGrainDirectory详解
c#
Liii40315 小时前
Java集合详细讲解
java·开发语言
落羽的落羽15 小时前
【C++】哈希扩展——位图和布隆过滤器的介绍与实现
linux·服务器·开发语言·c++·人工智能·算法·机器学习
fish_xk16 小时前
类和对象(二)
开发语言·c++·算法
武藤一雄16 小时前
.NET 中常见计时器大全
microsoft·微软·c#·.net·wpf·.netcore
lly20240616 小时前
Python 列表(List)详解
开发语言