WMI and CSharp (C#): Enumerate all Namespaces from Root

By | October 28, 2008

I like to understand WMI (Windows Management Instrumentation) as a database, main concepts are:

Namespaces: Databases

Classes: Tables

Properties: Columns

Instances: Lines

Values: Fields

With this in mind, we can start enumerating all the namespaces we can access locally.

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
            ManagementPath path = new ManagementPath(@"\localhostroot");
            ms.Path = path;

            ms.Connect();

            if (ms.IsConnected)
            //At this point we are connected to WMI
            {
                //Represents a management query that returns instances or classes
                ObjectQuery wql = new ObjectQuery("select * from __Namespace");

                //Retrieves a collection of 
                //management objects based on the specified query
                ManagementObjectSearcher searcher = 
                    new ManagementObjectSearcher(ms, wql);

                //Represents different collections 
               //of management objects retrieved through WMI
                ManagementObjectCollection oc = searcher.Get();

                //Represents the enumerator on the collection
                ManagementObjectCollection.ManagementObjectEnumerator oe =
                    oc.GetEnumerator();

                while (oe.MoveNext())
                {
                    foreach (PropertyData prop in oe.Current.Properties)
                    {
                        Console.WriteLine("t{0}", prop.Value);
                    }
                }
            }
            Console.ReadLine();
        }
    }
}

Running this program will display:

WMI Namespace Screenshoot

Next step is to enumerate all classes from a Namespace. This will come at next post :)

Ahmet

 
0 Kudos
Don't
move!



2 thoughts on “WMI and CSharp (C#): Enumerate all Namespaces from Root

Thoughts?