使用C#獲取操作系統(tǒng)版本號的四種方法
在 C# 中,可以通過 Environment.OSVersion 或 WMI (Windows Management Instrumentation) 獲取 Windows 10 或 Windows 11 的版本信息。但由于 Windows 11 修改了版本號格式(仍是 10.0),單獨使用 OSVersion 可能無法區(qū)分 Win10 和 Win11,需要額外的方法來判斷。
4 種方法檢測 Windows 10/11
| 方法 | 優(yōu)點 | 缺點 |
|---|---|---|
Environment.OSVersion | 簡單快速 | Win10/Win11 都返回 10.0 |
Registry | 直接讀取內(nèi)部版本號(22000+) | 需要訪問注冊表 |
WMI (Win32_OS) | 官方推薦方式 | 查詢較慢 |
IsWindows11OrGreater() (WinAPI) | 微軟官方方法(推薦) | 僅支持 .NET 5+ |
1、Environment.OSVersion(基礎(chǔ)判斷,無法區(qū)分 Win10/Win11)
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版本區(qū)分: - Windows 10:
< 22000 - Windows 11:
≥ 22000(首個正式版是 22000)
2、讀取注冊表(Get Build Number)
Win11 的內(nèi)部版本號 ≥ 22000:
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查詢)
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 包:
Install-Package System.Management
- WMI 查詢較慢,但可以獲取更多 OS 信息(如系統(tǒng)名稱、位數(shù))。
4、使用 WinAPIIsWindows11OrGreater()(推薦 .NET 5+)
如果你使用的是 .NET 5 / 6 / 7,可以直接調(diào)用 Windows API:
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;
}
}
// 調(diào)用
bool isWin11 = WinAPI.IsWindows11OrGreater();
Console.WriteLine(isWin11 ? "Windows 11" : "Windows 10");
? 優(yōu)點:
- 微軟官方推薦方法
- 精確判斷 Win11
? 適用 .NET 5 / 6 / 7
最終推薦方案
| 場景 | 推薦方法 |
|---|---|
| 簡單判斷(僅需 Build) | Registry / OSVersion.Build |
| 精確判斷(兼容 Win11) | IsWindows11OrGreater()(WinAPI) |
| 多 OS 信息(名稱、版本) | WMI (Win32_OS) |
最優(yōu)解
如果你的應(yīng)用 僅針對 Windows 10/11,推薦直接用注冊表或 Build 判斷:
int build = GetWinBuildNumber(); // 方法2 Console.WriteLine(build >= 22000 ? "Windows 11" : "Windows 10");
更簡單,無需復(fù)雜 API 調(diào)用!
額外(檢測 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!
以上就是使用C#獲取操作系統(tǒng)版本號的四種方法的詳細(xì)內(nèi)容,更多關(guān)于C#獲取系統(tǒng)版本號的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
C#操作SQLite數(shù)據(jù)庫方法小結(jié)
這篇文章介紹了C#操作SQLite數(shù)據(jù)庫的方法,文中通過示例代碼介紹的非常詳細(xì)。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-06-06

