-3

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

例如“Windows 8 Pro”我是指操作系统中的版本。

4

1 回答 1

0

您可以从注册表中获取操作系统名称,但您需要查询 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 获得更多信息,请务必查看MSDNWin32_OperatingSystem上的课程。

于 2013-09-27T19:54:21.537 回答