前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >WPF桌面端开发-获取系统版本,位数等信息

WPF桌面端开发-获取系统版本,位数等信息

作者头像
码客说
发布2023-07-11 14:09:19
5080
发布2023-07-11 14:09:19
举报
文章被收录于专栏:码客码客

获取系统版本

无论哪种方式获取系统版本,Win11获取到的都是Win10,但是版本号的方式可以通过构建号来判断Win11。

Management也能获取出Win11。

通过Version获取

注意:

通过版本号不能判断Win11,但是这里可以通过构建号来判断是否为Win11。

代码

代码语言:javascript
复制
/// <summary>
/// 通过版本号方式获取系统版本
/// </summary>
/// <returns></returns>
public static string GetOsVersion()
{
    OperatingSystem os = Environment.OSVersion;
    Version vs = os.Version;
    string operatingSystem = "";
    switch (os.Platform)
    {
        case PlatformID.Win32Windows:
            switch (vs.Minor)
            {
                case 0:
                    operatingSystem = "95";
                    break;
                case 10:
                    operatingSystem = vs.Revision.ToString() == "2222A" ? "98SE" : "98";
                    break;
                case 90:
                    operatingSystem = "Me";
                    break;
            }
            break;
        case PlatformID.Win32NT:
            switch (vs.Major)
            {
                case 3:
                    operatingSystem = "NT 3.51";
                    break;
                case 4:
                    operatingSystem = "NT 4.0";
                    break;
                case 5:
                    operatingSystem = vs.Minor == 0 ? "2000" : "XP";
                    break;
                case 6:
                    if (vs.Minor == 0)
                        operatingSystem = "Vista";
                    else if (vs.Minor == 1)
                        operatingSystem = "7";
                    else if (vs.Minor == 2)
                        operatingSystem = "8";
                    else
                        operatingSystem = "8.1";
                    break;
                case 10:
                    operatingSystem = "10";
                    if (os.Version.Build >= 22000)
                    {
                        operatingSystem = "11";
                    }
                    break;
            }
            break;
    }
    if (operatingSystem != "")
    {
        operatingSystem = "Windows " + operatingSystem;
        if (os.ServicePack != "")
        {
            operatingSystem += " " + os.ServicePack;
        }
    }
    return operatingSystem;
}

Win10获取的是Win8

此方法在Win10下获取的值可能不是10,这是因为版本不兼容,解决方案是程序应用清单中增加配置:

添加文件

image-20230630103702781
image-20230630103702781

添加后属性中已经默认选择了这个清单文件

image-20230630103847942
image-20230630103847942

把清单中的这些配置解除注释

代码语言:javascript
复制
<compatibility xmlns="urn:schemas-microsoft-com:compatibility.v1">
    <application>
      <!-- 设计此应用程序与其一起工作且已针对此应用程序进行测试的
           Windows 版本的列表。取消评论适当的元素,
           Windows 将自动选择最兼容的环境。 -->

      <!-- Windows Vista -->
      <supportedOS Id="{e2011457-1546-43c5-a5fe-008deee3d3f0}" />

      <!-- Windows 7 -->
      <supportedOS Id="{35138b9a-5d96-4fbd-8e2d-a2440225f93a}" />

      <!-- Windows 8 -->
      <supportedOS Id="{4a2f28e3-53b9-4441-ba9c-d69d4a4a6e38}" />

      <!-- Windows 8.1 -->
      <supportedOS Id="{1f676c76-80e1-4239-95bb-83d0f6d0da78}" />

      <!-- Windows 10 -->
      <supportedOS Id="{8e0f7a12-bfb3-4fe8-b9a5-48fd50a15a9a}" />

    </application>
</compatibility>

这时候就能正常获取Win10了。

版本号

Version

PlatformID

Major version

Minor version

Windows 95

Win32Windows

4

0

Windows 98

Win32Windows

4

10

Windows Me

Win32Windows

4

90

Windows NT 4.0

Win32NT

4

0

Windows 2000

Win32NT

5

0

Windows XP

Win32NT

5

1

Windows XP 64-Bit Edition

Win32NT

5

1

Windows 2003

Win32NT

5

2

Windows Server 2003

Win32NT

5

2

Windows Server 2003 R2

Win32NT

5

2

Windows 2003

Win32NT

5

2

Windows Vista

Win32NT

6

0

Windows 2008

Win32NT

6

0

Windows Server 2008

Win32NT

6

0

Windows 7

Win32NT

6

1

Windows 2008 R2

Win32NT

6

1

Windows Server 2008 R2

Win32NT

6

1

Windows 8

Win32NT

6

2

Windows Server 2012

Win32NT

6

2

Windows 8.1

Win32NT

6

3

Windows Server 2012 R2

Win32NT

6

3

Windows Server 2016 Technical Preview

Win32NT

10

0

Windows 10

Win32NT

10

0

Windows 11

Win32NT

10

0

通过注册表获取

这种方式Win10及以前都是准确的,但是

这种方式Win11会获取为Win10。

代码

代码语言:javascript
复制
/// <summary>
/// 获取Windows系统版本
/// </summary>
/// <returns>Windows系统版本字符串</returns>
public static string GetWindowsVersion()
{
    return (string)Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Windows NT\CurrentVersion")?.GetValue("ProductName");
}

通过InteropServices获取

注意:

这种方式Win11会获取为Win10。

添加引用

代码语言:javascript
复制
System.Runtime.InteropServices.Runtimelnformation

判断是否是Windows

代码语言:javascript
复制
RuntimeInformation.IsOSPlatform(OSPlatform.Windows)

获取系统版本

代码语言:javascript
复制
public static string GetSysVersion()
{
    string osVersion = RuntimeInformation.OSDescription;
    return osVersion;
}

Management方式(推荐)

注意

这种方式能获取到Win11。

添加引用

代码语言:javascript
复制
System.Management

代码

代码语言:javascript
复制
/// <summary>
/// 通过Management方式获取系统版本
/// </summary>
/// <returns></returns>
public static string GetOsNameInfo()
{
    try
    {
        ManagementClass osClass = new ManagementClass("Win32_OperatingSystem");
        foreach (var o in osClass.GetInstances())
        {
            var obj = (ManagementObject)o;
            PropertyDataCollection pdc = obj.Properties;
            foreach (PropertyData pd in pdc)
            {
                if (pd.Name == "Caption")
                {
                    return pd.Value.ToString()
                    .Replace(
                        "Microsoft ",
                        ""
                    );
                }
            }
        }
    }
    catch (Exception)
    {
        return "";
    }
    return "";
}

系统位数

代码语言:javascript
复制
/// <summary>
/// 获取系统位数
/// </summary>
/// <returns></returns>
public static int GetBit()
{
    return Environment.Is64BitOperatingSystem ? 64 : 32;
}

品牌型号

代码语言:javascript
复制
/// <summary>
/// 获取电脑品牌
/// </summary>
/// <returns></returns>
public static string GetPcManufacturer()
{
    // 电脑型号 Win32_ComputerSystem  Model
    //
    // 电脑品牌  Win32_ComputerSystem Manufacturer
    try
    {
        ManagementClass osClass = new ManagementClass("Win32_ComputerSystem");
        foreach (var o in osClass.GetInstances())
        {
            var obj = (ManagementObject)o;
            PropertyDataCollection pdc = obj.Properties;
            foreach (PropertyData pd in pdc)
            {
                if (pd.Name == "Manufacturer")
                {
                    return pd.Value.ToString().Replace("Microsoft ", "");
                }
            }
        }
    }
    catch (Exception)
    {
        return "";
    }
    return "";
}

/// <summary>
/// 获取电脑型号
/// </summary>
/// <returns></returns>
public static string GetPcModel()
{
    // 电脑型号 Win32_ComputerSystem  Model
    //
    // 电脑品牌  Win32_ComputerSystem Manufacturer
    try
    {
        ManagementClass osClass = new ManagementClass("Win32_ComputerSystem");
        foreach (var o in osClass.GetInstances())
        {
            var obj = (ManagementObject)o;
            PropertyDataCollection pdc = obj.Properties;
            foreach (PropertyData pd in pdc)
            {
                if (pd.Name == "Model")
                {
                    return pd.Value.ToString().Replace("Microsoft ", "");
                }
            }
        }
    }
    catch (Exception)
    {
        return "";
    }
    return "";
}

其他可获取的值

https://learn.microsoft.com/zh-cn/windows/win32/cimwin32prov/win32-computersystem?redirectedfrom=MSDN

常用的值

类型

Key

系统版本

Win32_OperatingSystem

Caption

电脑型号

Win32_ComputerSystem

Model

电脑品牌

Win32_ComputerSystem

Manufacturer

工作组

Win32_ComputerSystem

Domain

登录用户

Win32_ComputerSystem

UserName

计算机名

Win32_ComputerSystem

Name

最后盘符

Win32_BootConfiguration

LastDrive

主板序列号

Win32_ComputerSystemProduct

IdentifyingNumber

测试结果

代码语言:javascript
复制
private void GetSysInfo()
{
    List<string> strList = new List<string>();
    var sysInfo = ZSysUtils.GetSysVersion();
    strList.Add($@"InteropServices方式:{sysInfo}");
    strList.Add(@"============================");
    var windowsVersion = ZSysUtils.GetWindowsVersion();
    strList.Add($@"注册表方式:{windowsVersion}");
    strList.Add(@"============================");
    var osVersion = ZSysUtils.GetOsVersion();
    strList.Add($@"版本号方式:{osVersion}");
    strList.Add(@"============================");
    var osNameInfo = ZSysUtils.GetOsNameInfo();
    strList.Add($@"Management方式:{osNameInfo}");
    strList.Add(@"============================");
    strList.Add($@"系统位数:{ZSysUtils.GetBit()}");
    strList.Add(@"============================");
    var manufacturer = ZSysUtils.GetPcManufacturer();
    strList.Add($@"品牌:{manufacturer}");
    strList.Add(@"============================");
    var model = ZSysUtils.GetPcModel();
    strList.Add($@"型号:{model}");
    VersionTb.Text = string.Join(
        "\n",
        strList
    );
}

结果如下:

代码语言:javascript
复制
InteropServices方式:Windows 10.0.22621 
============================
注册表方式:Windows 10 Home China
============================
版本号方式:Windows 11
============================
Management方式:Windows 11 家庭中文版
============================
系统位数:64
============================
品牌:LENOVO
============================
型号:21CSA14YCD
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2023-06-30,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 获取系统版本
    • 通过Version获取
      • Win10获取的是Win8
      • 版本号
    • 通过注册表获取
      • 通过InteropServices获取
        • Management方式(推荐)
        • 系统位数
        • 品牌型号
        • 其他可获取的值
        • 测试结果
        相关产品与服务
        腾讯云服务器利旧
        云服务器(Cloud Virtual Machine,CVM)提供安全可靠的弹性计算服务。 您可以实时扩展或缩减计算资源,适应变化的业务需求,并只需按实际使用的资源计费。使用 CVM 可以极大降低您的软硬件采购成本,简化 IT 运维工作。
        领券
        问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档