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

相关推荐
猷咪21 分钟前
C++基础
开发语言·c++
IT·小灰灰22 分钟前
30行PHP,利用硅基流动API,网页客服瞬间上线
开发语言·人工智能·aigc·php
快点好好学习吧24 分钟前
phpize 依赖 php-config 获取 PHP 信息的庖丁解牛
android·开发语言·php
秦老师Q25 分钟前
php入门教程(超详细,一篇就够了!!!)
开发语言·mysql·php·db
烟锁池塘柳025 分钟前
解决Google Scholar “We‘re sorry... but your computer or network may be sending automated queries.”的问题
开发语言
是誰萆微了承諾25 分钟前
php 对接deepseek
android·开发语言·php
2601_9498683629 分钟前
Flutter for OpenHarmony 电子合同签署App实战 - 已签合同实现
java·开发语言·flutter
星火开发设计43 分钟前
类型别名 typedef:让复杂类型更简洁
开发语言·c++·学习·算法·函数·知识
qq_177767371 小时前
React Native鸿蒙跨平台数据使用监控应用技术,通过setInterval每5秒更新一次数据使用情况和套餐使用情况,模拟了真实应用中的数据监控场景
开发语言·前端·javascript·react native·react.js·ecmascript·harmonyos
一匹电信狗1 小时前
【LeetCode_21】合并两个有序链表
c语言·开发语言·数据结构·c++·算法·leetcode·stl