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