1 out of 2 rated this helpful - Rate this topic

How To: List the Classes in a WMI Namespace 

.NET Framework 2.0

This topic provides a complete example of how to browse the available classes in a WMI namespace.

Example

The following code example creates an application that allows the user to browse the available WMI classes in a specified namespace.

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Management;
using System.Data;

namespace SchemaBrowser 
{
    // This application browses WMI classes
    // available in local 
    // namespace entered by users.
    public class SchemaBrowerForm : 
        System.Windows.Forms.Form 
    {
        private System.Windows.Forms.Label label1;
        private System.Windows.Forms.Label label2;
        private System.Windows.Forms.TextBox namespaceValue;
        private System.Windows.Forms.ListBox classList;
        private System.Windows.Forms.Button searchButton;
        // Number of namespaces or classes found
        private int count;
        private System.Windows.Forms.Label label3;
        private System.Windows.Forms.Label statusValue;
        private System.ComponentModel.Container 
            components = null;

        public SchemaBrowerForm() 
        {
            count = 0;
            InitializeComponent();
        }

        protected override void Dispose( bool disposing ) 
        {
            if( disposing ) 
            {
                if (components != null) 
                {
                    components.Dispose();
                }
            }
            base.Dispose( disposing );
        }

        private void InitializeComponent()
        {
            this.classList = 
                new System.Windows.Forms.ListBox();
            this.searchButton = 
                new System.Windows.Forms.Button();
            this.label2 = new System.Windows.Forms.Label();
            this.namespaceValue = 
                new System.Windows.Forms.TextBox();
            this.label1 = new System.Windows.Forms.Label();
            this.statusValue = 
                new System.Windows.Forms.Label();
            this.label3 = new System.Windows.Forms.Label();
            this.SuspendLayout();
            // 
            // classList
            // 
            this.classList.Location = 
                new System.Drawing.Point(16, 64);
            this.classList.Name = "classList";
            this.classList.SelectionMode = 
                System.Windows.Forms.SelectionMode.None;
            this.classList.Size = 
                new System.Drawing.Size(264, 173);
            this.classList.Sorted = true;
            this.classList.TabIndex = 3;
            // 
            // searchButton
            // 
            this.searchButton.FlatStyle = 
                System.Windows.Forms.FlatStyle.Popup;
            this.searchButton.Location = 
                new System.Drawing.Point(224, 6);
            this.searchButton.Name = "searchButton";
            this.searchButton.Size = 
                new System.Drawing.Size(56, 24);
            this.searchButton.TabIndex = 2;
            this.searchButton.Text = "Search";
            this.searchButton.Click += 
                new System.EventHandler(this.button1_Click);
            // 
            // label2
            // 
            this.label2.Location = 
                new System.Drawing.Point(16, 45);
            this.label2.Name = "label2";
            this.label2.Size = 
                new System.Drawing.Size(48, 14);
            this.label2.TabIndex = 4;
            this.label2.Text = "Results:";
            // 
            // namespaceValue
            // 
            this.namespaceValue.Location = 
                new System.Drawing.Point(83, 8);
            this.namespaceValue.Name = "namespaceValue";
            this.namespaceValue.Size = 
                new System.Drawing.Size(128, 20);
            this.namespaceValue.TabIndex = 0;
            this.namespaceValue.Text = "root/default";
            // 
            // label1
            // 
            this.label1.Location = 
                new System.Drawing.Point(16, 10);
            this.label1.Name = "label1";
            this.label1.Size = 
                new System.Drawing.Size(68, 16);
            this.label1.TabIndex = 1;
            this.label1.Text = "Namespace:";
            // 
            // statusValue
            // 
            this.statusValue.BorderStyle = 
                System.Windows.Forms.
                BorderStyle.FixedSingle;
            this.statusValue.Location = 
                new System.Drawing.Point(56, 250);
            this.statusValue.Name = "statusValue";
            this.statusValue.Size = 
                new System.Drawing.Size(224, 18);
            this.statusValue.TabIndex = 6;
            // 
            // label3
            // 
            this.label3.Location = 
                new System.Drawing.Point(16, 251);
            this.label3.Name = "label3";
            this.label3.Size = 
                new System.Drawing.Size(40, 18);
            this.label3.TabIndex = 5;
            this.label3.Text = "Status:";
            // 
            // Form1
            // 
            this.AcceptButton = this.searchButton;
            this.AutoScaleBaseSize = 
                new System.Drawing.Size(5, 13);
            this.ClientSize = 
                new System.Drawing.Size(298, 279);
            this.Controls.AddRange(
                new System.Windows.Forms.Control[] {
                this.statusValue,
                this.label3,
                this.label2,
                this.classList,
                this.searchButton,
                this.label1,
                this.namespaceValue});
            this.FormBorderStyle = 
                System.Windows.Forms.
                FormBorderStyle.FixedSingle;
            this.MaximizeBox = false;
            this.Name = "Form1";
            this.StartPosition = 
                System.Windows.Forms.
                FormStartPosition.CenterScreen;
            this.Text = "Schema Browser";
            this.ResumeLayout(false);

        }

        // The main entry point for the application.
        [STAThread]
        static void Main() 
        {
            Application.Run(new SchemaBrowerForm());
        }

        private void button1_Click(
            object sender, System.EventArgs e) 
        {
            // Initialize class counter and
            // clear list view.
            count = 0;
            this.classList.Items.Clear();
            if (namespaceValue.Text.Equals("")) 
            {
                this.AddNamespacesToList();
            }
            else 
            {
                System.Threading.ThreadPool.
                    QueueUserWorkItem(
                    new System.Threading.WaitCallback(
                    this.AddClassesToList));
            }
        }

        private void AddNamespacesToList() 
        {
            this.statusValue.Text = "Searching...";
            try 
            {
                // Enumerate all WMI instances of 
                // __namespace WMI class.
                ManagementClass nsClass = 
                    new ManagementClass(
                    new ManagementScope("root"),
                    new ManagementPath("__namespace"),
                    null);
                foreach(ManagementObject ns in 
                    nsClass.GetInstances())
                {
                    this.classList.Items.
                        Add(ns["Name"].ToString());
                    count++;
                }
                this.statusValue.Text = 
                    count + " namespaces found.";
            }
            catch (ManagementException e) 
            {
                this.statusValue.Text = e.Message;
            }
        }

        private void AddClassesToList(object o) 
        {
            this.statusValue.Text = "Searching...";
            try 
            {
                // Perform WMI object query on 
                // selected namespace.
                ManagementObjectSearcher searcher = 
                    new ManagementObjectSearcher(
                    new ManagementScope(
                    namespaceValue.Text),
                    new WqlObjectQuery(
                    "select * from meta_class"),
                    null);
                foreach (ManagementClass wmiClass in
                    searcher.Get()) 
                {
                    this.classList.Items.Add(
                        wmiClass["__CLASS"].ToString());
                    count++;
                }
                this.statusValue.Text = 
                    count + " classes found.";
            }
            catch (ManagementException ex) 
            {
                this.statusValue.Text = ex.Message;
            }
        }
    }
}

See Also

Did you find this helpful?
(1500 characters remaining)
Community Content Add
Annotations FAQ