Pages

Friday, July 6, 2012

Windows Management Instrumentation (WMI)

Does anyone know how to get a list of the Win32_ classes in WMI like Win32_DiskDrive, Win32_Process, etc. programatically?

I was going to write a little application that lets you 'explore' WMI. The easy part is displaying the properties if you know the 'Win32_' class. Here is my code:

using System;
using System.Collections.Generic;
using System.Text;
using System.Management;
using System.ComponentModel;
using System.Runtime.InteropServices;

using System.Diagnostics;


namespace WmiProject
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.BufferHeight = 10000;
            Console.BufferWidth = 120;

            Console.WriteLine("**** Physical Disk Drives ******");
            using (ManagementObjectSearcher moSearcher = new ManagementObjectSearcher("SELECT * FROM Win32_DiskDrive"))
            {
                DisplayWin32ClassProperties(moSearcher);
            }

            Console.WriteLine("**** Logical Disk Drives ******");
            using (ManagementObjectSearcher moSearcher = new ManagementObjectSearcher("SELECT * FROM Win32_LogicalDisk"))
            {
                DisplayWin32ClassProperties(moSearcher);
            }

            Console.WriteLine("**** User Accounts ******");
            using (ManagementObjectSearcher moSearcher = new ManagementObjectSearcher("SELECT * FROM Win32_Account"))
            {
                DisplayWin32ClassProperties(moSearcher);
            }

            Console.WriteLine("**** Network Adapters ******");
            using (ManagementObjectSearcher moSearcher = new ManagementObjectSearcher("SELECT * FROM Win32_NetworkAdapter"))
            {
                DisplayWin32ClassProperties(moSearcher);
            }

            Console.WriteLine("**** Win32 Processes ******");
            using (ManagementObjectSearcher moSearcher = new ManagementObjectSearcher("SELECT * FROM Win32_Process"))
            {
                DisplayWin32ClassProperties(moSearcher);
            }

            Console.WriteLine("**** Win32 BIOS ******");
            using (ManagementObjectSearcher moSearcher = new ManagementObjectSearcher("SELECT * FROM Win32_BIOS"))
            {
                DisplayWin32ClassProperties(moSearcher);
            }

            Console.WriteLine("**** Operating System ******");
            using (ManagementObjectSearcher moSearcher = new ManagementObjectSearcher("SELECT * FROM Win32_OperatingSystem"))
            {
                DisplayWin32ClassProperties(moSearcher);
            }
        }


        static private void DisplayWin32ClassProperties(ManagementObjectSearcher moSearcher)
        {
                foreach (ManagementObject mo in moSearcher.Get())
                {
                    Console.WriteLine();
                    Console.WriteLine(mo.ToString());
                    PropertyDataCollection properties = mo.Properties;
                    foreach (PropertyData pd in properties)
                    {
                        Console.Write("    {0}  ", pd.Name);
                        if (pd.Value != null)
                        {
                            Console.WriteLine(pd.Value.ToString());
                        }
                        else
                        {
                            Console.WriteLine("null");
                        }
                    }
                    Console.WriteLine("\n press any key to continue");
                    Console.Read();
                }
        }
    }
}

No comments:

Post a Comment