简体   繁体   中英

Get CIM Data type from properties in WMI Classes with C#

So I've managed to write a class that allows me to access WMI and get information about classes, including their methods, and all of the properties of the classes and of their subsequent methods. I am unable to find anything in C# under the System.Management or System.Management.Instrumentation classes that allows me to access the CIM data types of properties in WMI, either in the main class, or in methods. Does anyone know of a way that I can get those data types?

To get the metadata (like cimtype, value, name) of the WMI classes you can use the PropertyData class.

Try this sample code from 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();
            }
        }    
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM