如何找到Windows版本名称

本文关键字:版本 Windows 何找 | 更新日期: 2023-09-27 18:33:02

如何为我的 c# 应用程序找到Microsoft Windows(操作系统名称)。

例如"Windows 8 Pro",我的意思是操作系统中的版本。

如何找到Windows版本名称

可以从注册表中获取操作系统名称,但需要查询 WMI 以获取体系结构和服务包信息:

using System.Diagnostics;
...
private string GetOperatingSystemInfo()
{
    RegistryKey operatingSystemKey = Registry.LocalMachine.OpenSubKey(@"SOFTWARE'Microsoft'Windows NT'CurrentVersion");
    string operatingSystemName = operatingSystemKey.GetValue("ProductName").ToString();
    ConnectionOptions options = new ConnectionOptions();
    // query any machine on the network     
    ManagementScope scope = new ManagementScope("''''machineName''root''cimv2", options);
    scope.Connect();
    // define a select query
    SelectQuery query = new SelectQuery("SELECT OSArchitecture, CSDVersion FROM Win32_OperatingSystem");
    ManagementObjectSearcher searcher = new ManagementObjectSearcher(scope, query);
    string osArchitecture = "";
    string osServicePack = "";
    foreach (ManagementObject mo in searcher.Get())
    {
        osArchitecture = mo["OSArchitecture"].ToString();
        osServicePack = mo["CSDVersion"].ToString();               
    }            
    return operatingSystemName + " " + osArchitecture + " " + osServicePack;                         
}

如果需要从 WMI 获得更多信息,请务必查看 MSDN 上的Win32_OperatingSystem类。