Now that we know how to enumerate all the namespace from root, let’s have a look on how to enumerate all the classes from a namespace.

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

namespace WmiNamespace
{
    class Program
    {
        static void Main(string[] args)
        {
            //Represents the scoop for management operations
            ManagementScope ms = new ManagementScope();

            //Provides a wrapper for building paths to WMI objects
            //Here we connect to the namespace called "CIMV2"
            ManagementPath path = new ManagementPath(@"\\localhost\root\CIMV2");

            //Represents a CIM management class
            ManagementClass newClass = new ManagementClass(path);

            //Provides a base class for query and enumeration-related options objects
            EnumerationOptions options = new EnumerationOptions();

            //Return class members that are immediately related to the class
            options.EnumerateDeep = false;

            //Retrieve the  sbuclasses using the specified options
            foreach (ManagementObject o in newClass.GetSubclasses(options))
            {
                Console.WriteLine(Convert.ToString(o["__Class"]));
            }
            Console.ReadLine();
        }
    }
}

You should see this:

wmi Namespace Classes Screenshoot

Next step is to enumerate all the instances from a class.

Ahmet

  2 Responses to “WMI and CSharp (C#):
Enumerate all classes from a namespace”

  1. [...] the two previous posts (Enumerate all classes from a namespace and Enumerate all Namespaces from Root) we gather enough information to enumerate namespaces and [...]

  2. Thanks, very helpfull!!!

 Leave a Reply

(required)

(required)

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>