如何找到Windows Edition名称

时间:2013-09-27 19:47:35

标签: c# windows operating-system

如何为我的c#应用程序找到Microsoft Windows (Os名称)

如'Windows 8 Pro'我的意思是来自操作系统的版本。

1 个答案:

答案 0 :(得分:0)

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

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课程。