C# WMI查找值
我正在使用WMI检索服务器操作系统的信息。我有以下查询->C# WMI查找值,c#,wmi,C#,Wmi,我正在使用WMI检索服务器操作系统的信息。我有以下查询-> select * from Win32_OperatingSystem 结果很好,但是对于OperatingSystemSKU,它返回一个整数。在microsoft网站上,它提供了实际值。我想知道这个查找表是否存在,或者我是否需要创建自己的本地表来进行映射 这是我不久前写的一个(非常精简的)方法-使用Environment.OSVersion获取名称,并将windows版本作为字符串获取-我只是假设您希望在此处使用操作系统版本名称,
select * from Win32_OperatingSystem
结果很好,但是对于OperatingSystemSKU,它返回一个整数。在microsoft网站上,它提供了实际值。我想知道这个查找表是否存在,或者我是否需要创建自己的本地表来进行映射
这是我不久前写的一个(非常精简的)方法-使用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!
}
}
}
//
///获取环境信息的类
///
公共静态类环境信息
{
///
///通过查询系统获取环境信息
///
公共静态IEnumerable GetEnvironmentInfo()
{
列表结果=新列表();
SafeUpdateListOfResultsFromInstrumentation(“操作系统产品:{0}”,结果,“从win32_操作系统中选择*”,“名称”);
SafeUpdateListofResults(“OS版本:{0}”,结果,(()=>Environment.OSVersion.ToString());
返回结果;
}
私有静态void SafeUpdateListofResults(字符串格式、列表结果、Func del)
{
尝试
{
string str=del.Invoke();
Add(string.Format(Format,str));
}
捕获(例外)
{
//吞咽异常-无法获取诊断信息!
}
}
私有静态void SafeUpdateListOfResultsFromInstrumentation(字符串格式、列表结果、字符串查询、字符串索引)
{
尝试
{
WqlObjectQuery objectQuery=新的WqlObjectQuery(查询);
ManagementObjectSearcher search=新的ManagementObjectSearcher(objectQuery);
字符串名称、值;
foreach(searcher.Get()中的ManagementObject ManagementObject)
{
name=managementObject[index].ToString();
字符串[]split1=name.Split(“|”);
值=拆分1[0];
Add(string.Format(Format,value));
}
}
捕获(例外)
{
//吞咽异常-无法获取诊断信息!
}
}
}
您可以这样查找它
string OSname = (from x in new ManagementObjectSearcher("SELECT * FROM Win32_OperatingSystem").Get().OfType<ManagementObject>() select x.GetPropertyValue("Caption")).First().ToString();
string OSname=(从新ManagementObjectSearcher中的x(“从Win32_OperatingSystem中选择*).Get().OfType()选择x.GetPropertyValue(“标题”).First().ToString();
或者只是使用一个消息框
MessageBox.Show((from x in new ManagementObjectSearcher("SELECT * FROM Win32_OperatingSystem").Get().OfType<ManagementObject>() select x.GetPropertyValue("Caption")).First().ToString());
MessageBox.Show((从新的ManagementObjectSearcher中的x(“从Win32\U操作系统中选择*).Get().OfType()选择x.GetPropertyValue(“标题”).First().ToString());
您可以为访问值
限定符的操作系统库
属性提取有效值
检查下一幅图像(使用获得),其中显示了OperatingSystemSKU
属性的所有限定符和值
从.Net中,您必须使用该类来提取此类信息
检查下一个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();
}
}
}
使用系统;
使用System.Collections.Generic;
使用制度管理;
使用系统文本;
命名空间GetWMI\u信息
{
班级计划
{
静态void Main(字符串[]参数)
{
尝试
{
List sLookUp=新列表();
ManagementClass manClass=新的管理类(“Win32_操作系统”);
manClass.Options.UseAmendedQualifiers=true;
foreach(manClass.Properties中的PropertyData属性)
if(Property.Name.Equals(“OperatingSystemSKU”))
foreach(Property.Qualifiers中的限定符数据限定符)
if(Qualifier.Name.Equals(“值”))
foreach(System.String[])Qualifier.Value中的字符串s)
sLookUp.Add(s);
管理范围;
Scope=newmanagementscope(String.Format(“\\{0}\\root\\CIMV2”,”),null);
Scope.Connect();
ObjectQuery查询=新建ObjectQuery(“从Win32_OperatingSystem中选择OperatingSystemSKU”);
ManagementObjectSearcher search=新的ManagementObjectSearcher(范围、查询);
foreach(Searcher.Get()中的ManagementObject WMIOObject)
{
Console.WriteLine(“{0}{1}”,“OperatingSystemSKU”,sLookUp[Convert.ToInt32((UInt32)WMIOObject[“OperatingSystemSKU”]))];//UInt32
}
}
捕获(例外e)
{
WriteLine(String.Format(“异常{0}跟踪{1}”,e.Message,e.StackTrace));
}
控制台写入线(“按回车键退出”);
Console.Read();
}
}
}
Caption属性如何?它似乎包含本地化字符串中的信息。