You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

.NET 6中如何正确获取Windows系统版本、版本类型及语言信息?

在.NET 6中获取Windows系统信息的正确方法

一、获取完整系统版本及版本类型(如Enterprise)

Environment.OSVersion和RuntimeInformation无法返回版本类型(如Enterprise),且Windows 10+下版本号返回存在兼容性问题,推荐通过以下两种方式获取完整信息:

方法1:P/Invoke调用原生API(无额外依赖)

通过RtlGetVersion获取准确版本号,GetProductInfo获取版本类型,结合RuntimeInformation获取架构:

using System;
using System.Runtime.InteropServices;

public static class WindowsSystemInfo
{
    [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;
    }

    [DllImport("ntdll.dll", SetLastError = true)]
    private static extern uint RtlGetVersion(ref OSVERSIONINFOEX lpVersionInformation);

    [DllImport("kernel32.dll", SetLastError = true)]
    private static extern bool GetProductInfo(uint dwOSMajorVersion, uint dwOSMinorVersion, uint dwSpMajorVersion, uint dwSpMinorVersion, out uint pdwReturnedProductType);

    public static string GetFullSystemInfo()
    {
        var osVersionInfo = new OSVERSIONINFOEX { dwOSVersionInfoSize = (uint)Marshal.SizeOf(typeof(OSVERSIONINFOEX)) };
        RtlGetVersion(ref osVersionInfo);

        // 映射版本类型
        GetProductInfo(osVersionInfo.dwMajorVersion, osVersionInfo.dwMinorVersion, osVersionInfo.wServicePackMajor, osVersionInfo.wServicePackMinor, out var productType);
        string edition = productType switch
        {
            0x00000030 => "Enterprise",
            0x0000001B => "Pro",
            0x0000001A => "Home",
            0x00000027 => "Education",
            _ => "Unknown"
        };

        // 获取系统架构
        string architecture = RuntimeInformation.OSArchitecture switch
        {
            Architecture.X64 => "64 bit",
            Architecture.X86 => "32 bit",
            Architecture.Arm64 => "ARM64 bit",
            _ => "Unknown"
        };

        // 判断Windows版本名称
        string osName = osVersionInfo.dwMajorVersion switch
        {
            10 when osVersionInfo.dwBuildNumber >= 22000 => "Windows 11",
            10 => "Windows 10",
            6 when osVersionInfo.dwMinorVersion == 3 => "Windows 8.1",
            6 when osVersionInfo.dwMinorVersion == 2 => "Windows 8",
            6 when osVersionInfo.dwMinorVersion == 1 => "Windows 7",
            _ => $"Windows {osVersionInfo.dwMajorVersion}.{osVersionInfo.dwMinorVersion}"
        };

        return $"{osName} {edition} build {osVersionInfo.dwBuildNumber} ({architecture})";
    }
}

调用示例:

Console.WriteLine(WindowsSystemInfo.GetFullSystemInfo());
// 输出示例:Windows 10 Enterprise build 22000 (64 bit)

方法2:WMI查询(需安装NuGet包)

引入System.Management NuGet包,通过查询Win32_OperatingSystem类获取信息:

先安装包:

Install-Package System.Management

代码实现:

using System.Management;

public static string GetFullSystemInfoViaWmi()
{
    using var searcher = new ManagementObjectSearcher("SELECT Caption, BuildNumber, OSArchitecture, OperatingSystemSKU FROM Win32_OperatingSystem");
    foreach (var obj in searcher.Get())
    {
        string caption = obj["Caption"]?.ToString() ?? "Unknown Windows Version";
        string buildNumber = obj["BuildNumber"]?.ToString() ?? "Unknown";
        string architecture = obj["OSArchitecture"]?.ToString() ?? "Unknown";
        uint sku = (uint)(obj["OperatingSystemSKU"] ?? 0);
        
        string edition = sku switch
        {
            4 => "Home",
            48 => "Pro",
            40 => "Enterprise",
            12 => "Education",
            _ => "Unknown"
        };

        string osName = caption.Split(new[] { " " }, 3)[0] + " " + caption.Split(new[] { " " }, 3)[1];
        return $"{osName} {edition} build {buildNumber} ({architecture})";
    }
    return "Failed to retrieve system info";
}

二、获取Windows安装语言

安装语言是系统初始安装时的语言,通过GetSystemDefaultLangID API获取:

using System;
using System.Runtime.InteropServices;
using System.Globalization;

public static class WindowsLanguageInfo
{
    [DllImport("kernel32.dll", CharSet = CharSet.Auto)]
    private static extern ushort GetSystemDefaultLangID();

    public static string GetInstallLanguage()
    {
        ushort langId = GetSystemDefaultLangID();
        var culture = new CultureInfo(langId);
        return $"{culture.DisplayName} ({culture.Name})";
    }
}

调用示例:

Console.WriteLine("安装语言:" + WindowsLanguageInfo.GetInstallLanguage());
// 输出示例:中文(简体) (zh-CN)

三、获取当前系统语言(用户UI语言)

当前系统语言是用户界面使用的语言,有两种获取方式:

方法1:使用CultureInfo类

using System.Globalization;

public static string GetCurrentSystemLanguage()
{
    var uiCulture = CultureInfo.CurrentUICulture;
    return $"{uiCulture.DisplayName} ({uiCulture.Name})";
}

方法2:调用Windows API(避免线程文化影响)

using System;
using System.Runtime.InteropServices;
using System.Globalization;

public static class WindowsLanguageInfo
{
    [DllImport("user32.dll", CharSet = CharSet.Auto)]
    private static extern int GetUserDefaultUILanguage();

    public static string GetCurrentSystemLanguageViaApi()
    {
        int langId = GetUserDefaultUILanguage();
        var culture = new CultureInfo(langId);
        return $"{culture.DisplayName} ({culture.Name})";
    }
}

调用示例:

Console.WriteLine("当前系统语言:" + WindowsLanguageInfo.GetCurrentSystemLanguage());
// 输出示例:中文(简体) (zh-CN)

内容的提问来源于stack exchange,提问作者KUMI

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.01 20:20:55