【问题标题】:Get CIM Data type from properties in WMI Classes with C#使用 C# 从 WMI 类中的属性获取 CIM 数据类型
【发布时间】:2014-11-25 19:28:39
【问题描述】:

所以我设法编写了一个类,它允许我访问 WMI 并获取有关类的信息,包括它们的方法,以及类及其后续方法的所有属性。我无法在 C# 中的 System.Management 或 System.Management.Instrumentation 类下找到任何允许我在主类或方法中访问 WMI 中属性的 CIM 数据类型的内容。有谁知道我可以获得这些数据类型的方法吗?

【问题讨论】:

  • 那里似乎没有任何东西可以让我获取 WMI 类中属性的数据类型。此外,对于我正在研究的类,CIM_DataFile 似乎并不位于层次结构的顶部。我还不是 WMI/CIM/WBEM 方面的专家,但我很确定这不是我想要的。

标签: c# wmi information-retrieval system.management wbem


【解决方案1】:

要获取 WMI 类的元数据(如 cimtype、值、名称),您可以使用 PropertyData 类。

试试这个来自 MSDN 的示例代码

using System;
using System.Management;

public class Sample 
{    
    public static void Main() 
    {

        // Get the WMI class
        ManagementClass osClass = 
            new ManagementClass("Win32_OperatingSystem");

        osClass.Options.UseAmendedQualifiers = true;

        // Get the Properties in the class
        PropertyDataCollection properties =
            osClass.Properties;

        // display the Property names
        Console.WriteLine("Property Name: ");
        foreach (PropertyData property in properties)
        {
            Console.WriteLine(
                "---------------------------------------");
            Console.WriteLine(property.Name);
            Console.WriteLine("Description: " + property.Qualifiers["Description"].Value);
            Console.WriteLine();

            Console.WriteLine("Type: ");               
            Console.WriteLine(property.Type);

            Console.WriteLine();

            Console.WriteLine("Qualifiers: ");
            foreach(QualifierData q in 
                property.Qualifiers)
            {
                Console.WriteLine(q.Name);
            }
            Console.WriteLine();

            foreach (ManagementObject c in osClass.GetInstances())
            {
                Console.WriteLine("Value: ");
                Console.WriteLine(c.Properties[property.Name.ToString()].Value);
                Console.WriteLine();
            }
        }    
    }

【讨论】:

  • 谢谢,这行得通。我认为这是 ManagementClass 对象的 Properties 属性,但我很难使用其中包含的数据。 System.Management 命名空间仍然是新的。再次感谢!编辑:
猜你喜欢
  • 1970-01-01
  • 2020-01-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-09-28
相关资源
最近更新 更多