0

我正在使用 WMI 检索服务器操作系统的信息。我有以下查询->

select *  from Win32_OperatingSystem

结果很好,但是对于 OperatingSystemSKU,它返回一个整数。在 microsoft 网站上,它提供了实际值。我想知道这个查找表是否存在,还是我必须创建自己的本地表来进行映射?

http://msdn.microsoft.com/en-us/library/windows/desktop/aa394239(v=vs.85).aspx

4

3 回答 3

1

你可以这样查找

string OSname = (from x in new ManagementObjectSearcher("SELECT * FROM Win32_OperatingSystem").Get().OfType<ManagementObject>() select x.GetPropertyValue("Caption")).First().ToString();

或者只是使用一个消息框。

MessageBox.Show((from x in new ManagementObjectSearcher("SELECT * FROM Win32_OperatingSystem").Get().OfType<ManagementObject>() select x.GetPropertyValue("Caption")).First().ToString());
于 2013-02-05T08:55:52.730 回答
1

OperatingSystemSKU您可以提取访问Values限定符的属性的有效值。

检查下一张图片(使用WDCC获得),其中显示了该属性的所有限定符和值 OperatingSystemSKU

在此处输入图像描述

从 .Net 中,您必须使用QualifierData类来提取此类信息。

检查下一个使用限定符值构建查找列表的 C# 示例。

using System;
using System.Collections.Generic;
using System.Management;
using System.Text;

namespace GetWMI_Info
{
    class Program
    {

        static void Main(string[] args)
        {
            try
            {
                List<string> sLookUp = new List<string>();
                ManagementClass manClass = new ManagementClass("Win32_OperatingSystem");
                manClass.Options.UseAmendedQualifiers = true;
                foreach (PropertyData Property in manClass.Properties)
                    if (Property.Name.Equals("OperatingSystemSKU"))                    
                        foreach (QualifierData Qualifier in Property.Qualifiers)
                            if (Qualifier.Name.Equals("Values")) 
                                foreach (String s in (System.String[])Qualifier.Value)
                            sLookUp.Add(s);                   


                ManagementScope Scope;                
                Scope = new ManagementScope(String.Format("\\\\{0}\\root\\CIMV2", "."), null);
                Scope.Connect();
                ObjectQuery Query = new ObjectQuery("SELECT OperatingSystemSKU FROM Win32_OperatingSystem");
                ManagementObjectSearcher Searcher = new ManagementObjectSearcher(Scope, Query);

                foreach (ManagementObject WmiObject in Searcher.Get())
                {
                    Console.WriteLine("{0} {1}", "OperatingSystemSKU", sLookUp[Convert.ToInt32((UInt32)WmiObject["OperatingSystemSKU"])]);// Uint32
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(String.Format("Exception {0} Trace {1}",e.Message,e.StackTrace));
            }
            Console.WriteLine("Press Enter to exit");
            Console.Read();
        }
    }
}
于 2013-02-05T19:47:59.550 回答
0

这是我前段时间写的一个(非常精简的)方法 - 获取名称并使用 Environment.OSVersion 将 Windows 版本作为字符串获取 - 我只是假设您想要这里的操作系统版本名称而不是 sku?

/// <summary>
/// Class for getting environment information
/// </summary>
public static class EnvironmentInfo
{
    /// <summary>
    /// Gets environment information by querying the system
    /// </summary>
    public static IEnumerable<string> GetEnvironmentInfo()
    {
        List<string> results = new List<string>();

        SafeUpdateListOfResultsFromInstrumentation("OS Product: {0}", results, "select * from win32_OperatingSystem", "name");
        SafeUpdateListofResults("OS Version: {0}", results, (() => Environment.OSVersion.ToString()));

        return results;
    }

    private static void SafeUpdateListofResults(string format, List<string> results, Func<string> del)
    {
        try
        {
            string str = del.Invoke();
            results.Add(string.Format(format, str));
        }
        catch (Exception)
        {
            //Swallow exception - can't get diagnostic info!
        }
    }

    private static void SafeUpdateListOfResultsFromInstrumentation(string format, List<string> results, string query, string index)
    {
        try
        {
            WqlObjectQuery objectQuery = new WqlObjectQuery(query);
            ManagementObjectSearcher searcher = new ManagementObjectSearcher(objectQuery);

            string name, value;

            foreach (ManagementObject managementObject in searcher.Get())
            {
                name = managementObject[index].ToString();
                string[] split1 = name.Split('|');
                value = split1[0];
                results.Add(string.Format(format, value));
            }
        }
        catch (Exception)
        {
            //Swallow exception - can't get diagnostic info!
        }

    }
}
于 2013-02-05T08:50:51.507 回答