Consultation du schéma WMI
L'exemple de code suivant montre une application qui permet à l'utilisateur de parcourir les classes WMI disponibles dans un espace de noms spécifié :
using System;
using System.Collections;
using System.ComponentModel;
using System.Management;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
namespace SchemaBrowser
{
/// <summary>
/// This application browses WMI classes available in the local namespace entered.
/// </summary>
public class WebForm1 : System.Web.UI.Page {
protected System.Web.UI.WebControls.Label Label1;
protected System.Web.UI.WebControls.TextBox namespaceValue;
protected System.Web.UI.WebControls.Button searchButton;
protected System.Web.UI.WebControls.ListBox classList;
protected System.Web.UI.WebControls.Label Label3;
protected System.Web.UI.WebControls.Label statusValue;
protected System.Web.UI.WebControls.Label Label2;
private int count;
public WebForm1() {
Page.Init += new System.EventHandler(Page_Init);
}
private void Page_Init(object sender, EventArgs e) {
count = 0;
InitializeComponent();
}
private void InitializeComponent() {
this.searchButton.Click +=
new System.EventHandler(this.searchButton_Click);
}
private void searchButton_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 {
this.AddClassesToList();
}
}
private void AddNamespacesToList() {
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 (Exception e) {
this.statusValue.Text = e.Message;
}
}
private void AddClassesToList() {
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 (Exception ex) {
this.statusValue.Text = ex.Message;
}
}
}
}
[Visual Basic]
Imports System
Imports System.Collections
Imports System.ComponentModel
Imports System.Management
Imports System.Drawing
Imports System.Web
Imports System.Web.SessionState
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Web.UI.HtmlControls
Namespace SchemaBrowser
' This application browses WMI classes available in local
' namespace entered.
Public Class WebForm1
Inherits System.Web.UI.Page
Protected Label1 As System.Web.UI.WebControls.Label
Protected namespaceValue As System.Web.UI.WebControls.TextBox
Protected searchButton As System.Web.UI.WebControls.Button
Protected classList As System.Web.UI.WebControls.ListBox
Protected Label3 As System.Web.UI.WebControls.Label
Protected statusValue As System.Web.UI.WebControls.Label
Protected Label2 As System.Web.UI.WebControls.Label
Private count As Integer
Public Sub New()
AddHandler Page.Init, AddressOf Page_Init
End Sub
Private Sub Page_Init(sender As Object, e As EventArgs)
count = 0
InitializeComponent()
End Sub
Private Sub InitializeComponent()
AddHandler searchButton.Click, AddressOf Me.searchButton_Click
End Sub
Private Sub searchButton_Click(sender As Object, e As System.EventArgs)
' Initialize class counter and clear list view.
Me.statusValue.Text = "!!!"
count = 0
Me.classList.Items.Clear()
If namespaceValue.Text.Equals("") Then
Me.AddNamespacesToList()
Else
Me.AddClassesToList()
End If
End Sub
Private Sub AddNamespacesToList()
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 Exception
Me.statusValue.Text = e.Message
End Try
End Sub
Private Sub AddClassesToList()
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 Exception
Me.statusValue.Text = ex.Message
End Try
End Sub
End Class
End Namespace