Evaluar y enviar comentarios
Esta página es específica de
Microsoft Visual Studio 2005/.NET Framework 2.0

Hay además otras versiones disponibles para:
Cómo enumerar las clases en un espacio de nombres WMI

Este tema proporciona un ejemplo completo de cómo explorar las clases disponibles en un espacio de nombres WMI.

Ejemplo

En el ejemplo de código siguiente se crea una aplicación que permite al usuario explorar las clases WMI disponibles en un espacio de nombres especificado.

Visual Basic
Imports System
Imports System.Drawing
Imports System.Collections
Imports System.ComponentModel
Imports System.Windows.Forms
Imports System.Management
Imports System.Threading

Namespace SchemaBrowser
    ' This application browses WMI classes
    ' available in local 
    ' namespace entered by users.
    Public Class SchemaBrowerForm
        Inherits Form
        Private label1 As System.Windows.Forms.Label
        Private label2 As System.Windows.Forms.Label
        Private namespaceValue As _
            System.Windows.Forms.TextBox
        Private classList As _
            System.Windows.Forms.ListBox
        Private searchButton _
            As System.Windows.Forms.Button
        ' Number of namespaces or classes found
        Private count As Integer
        Private label3 As System.Windows.Forms.Label
        Private statusValue As _
            System.Windows.Forms.Label
        Private components As _
            System.ComponentModel.Container = Nothing

        Public Sub New()
            count = 0
            InitializeComponent()
        End Sub

        Protected Overloads Sub Dispose( _
        ByVal disposing As Boolean)

            If disposing Then
                If Not (components Is Nothing) Then
                    components.Dispose()
                End If
            End If
            MyBase.Dispose(disposing)
        End Sub

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

        <STAThread()> _
        Shared Sub Main()
            Application.Run(New SchemaBrowerForm)
        End Sub

        Private Sub button1_Click( _
        ByVal sender As Object, ByVal e As System.EventArgs)
            ' Initialize class counter and
            ' clear list view.
            count = 0
            Me.classList.Items.Clear()
            If namespaceValue.Text.Equals("") Then
                Me.AddNamespacesToList()
            Else
                ThreadPool.QueueUserWorkItem( _
                 New WaitCallback( _
                     AddressOf Me.AddClassesToList))
            End If
        End Sub

        Private Sub AddNamespacesToList()
            Me.statusValue.Text = "Searching..."
            Try
                ' Enumerate all WMI instances of 
                ' __namespace WMI class.
                Dim nsClass As New ManagementClass( _
                 New ManagementScope("root"), _
                 New ManagementPath("__namespace"), _
                 Nothing)
                Dim ns As ManagementObject
                For Each ns In nsClass.GetInstances()
                    Me.classList.Items.Add(ns("Name"). _
                        ToString())
                    count += 1
                Next ns
                Me.statusValue.Text = _
                    count & " namespaces found."
            Catch e As ManagementException
                Me.statusValue.Text = e.Message
            End Try
        End Sub

        Private Sub AddClassesToList(ByVal o As Object)
            Me.statusValue.Text = "Searching..."
            Try
                ' Perform WMI object query on
                ' selected namespace.
                Dim searcher As New _
                     ManagementObjectSearcher( _
                 New ManagementScope( _
                     namespaceValue.Text), _
                 New WqlObjectQuery( _
                     "select * from meta_class"), _
                 Nothing)
                Dim wmiClass As ManagementClass
                For Each wmiClass In searcher.Get()
                    Me.classList.Items.Add( _
                        wmiClass("__CLASS").ToString())
                    count += 1
                Next wmiClass
                Me.statusValue.Text = _
                                count & " classes found."
            Catch ex As ManagementException
                Me.statusValue.Text = ex.Message
            End Try
        End Sub
    End Class
End Namespace
C#
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;
            }
        }
    }
}

Vea también

Contenido de la comunidad   ¿Qué es Community Content?
Agregar contenido nuevo RSS  Anotaciones
Processing
© 2009 Microsoft Corporation. Reservados todos los derechos. Términos de uso | Marcas Registradas | Privacidad
Page view tracker