在 C# 中,可以通过 Environment.OSVersion 或 WMI (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());
📌 注意:
- 需要
System.ManagementNuGet 包:
bash
Install-Package System.Management
- 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!