如何使用C#来读取的ManagementObject集合WMI [英] How to read ManagementObject Collection in WMI using C#

查看:3534
本文介绍了如何使用C#来读取的ManagementObject集合WMI的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现网上有一个代码,并一直试图获得关于月[]



我想获得的所有详细信息包含在ManagementObjectCollection的信息。



由于寻找一个字符串值,我不知道,我怎么可以得到所有的值参数不知道它的参数值。或者,如果我想在 ManagementObjectCollection

相关莫所有的索引值

  ManagementObjectSearcher objOSDetails =新ManagementObjectSearcher(SELECT * FROM Win32_OperatingSystem); 
ManagementObjectCollection osDetailsCollection = objOSDetails.Get();

的foreach(在osDetailsCollection的ManagementObject MO)
{
_osName = MO [名称]的ToString(); //其他什么领域都还有比名$ B $其它b _osVesion = MO [版本]的ToString()。
_loginName = MO [csname]的ToString()。
}


解决方案

看看你的WMI查询:

  SELECT * FROM Win32_OperatingSystem 

这意味着得到 Win32_OperatingSystem 类,并包括所有的类属性。这是一个线索,所产生的的ManagementObject 是在WMI Win32_OperatingSystem 类包装。请参阅类描述,了解什么样的属性有,他们的意思,并决定哪些你。实际上需要在你的代码中使用



如果您需要通过所有可用的属性,而无需硬编码自己的名字,使用的 属性 财产作为一样的 Giorgi的建议。这里有一个例子:

 的foreach(在osDetailsCollection的ManagementObject MO)
{
的foreach(PropertyData道具莫的.properties)
{
Console.WriteLine({0}:{1},prop.Name,prop.Value);
}
}


I found a code on net and have been trying to get more information about mo[].

I am trying to get all the information contained in ManagementObjectCollection.

Since parameter in mo is looking for an string value which I dont know, how can I get all the values without knowing its parameter values. Or if I want to get all the indexer values related to mo in ManagementObjectCollection

ManagementObjectSearcher objOSDetails = new ManagementObjectSearcher("SELECT * FROM Win32_OperatingSystem");
ManagementObjectCollection osDetailsCollection = objOSDetails.Get();

foreach( ManagementObject mo in osDetailsCollection )
{ 
   _osName  = mo["name"].ToString();// what other fields are there other than name
   _osVesion = mo["version"].ToString();
   _loginName = mo["csname"].ToString();
}

解决方案

Take a look at your WMI query:

SELECT * FROM Win32_OperatingSystem

It means "get all instances of the Win32_OperatingSystem class and include all class properties". This is a clue that the resulting ManagementObjects are wrappers over the WMI Win32_OperatingSystem class. See the class description to learn what properties it has, what they mean and to decide which ones you actually need to use in your code.

If you need to iterate through all available properties without hard-coding their names, use the Properties property like as Giorgi suggested. Here's an example:

foreach (ManagementObject mo in osDetailsCollection)
{
    foreach (PropertyData prop in mo.Properties)
    {
        Console.WriteLine("{0}: {1}", prop.Name, prop.Value);
    }
}

这篇关于如何使用C#来读取的ManagementObject集合WMI的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆