다음을 통해 공유


VB.Net - Binding WMI ManagementObjectSearcher results

Overview

The System.Management namespace contains some very useful system information that can't always be found in an easier way. Recently a question was posed about querying WMI for 2 Properties, the first being listed in a ComboBox, and the second displayed in a TextBox, dependent on the selection in the ComboBox. Initially this seems a perfect case for DataBinding, but after some experimentation it was found that ManagementObjectSearcher results can't be easily bound. 

The solution to the 2 Property question was to use a Dictionary(of String, String), with the Keys being DisplayName, and the Value PathName.

After considering more than 2 Properties might be more useful, and some more experimenting with binding, a multi-Property binding solution was found by wrapping the ManagementObject objects in a wrapper class, with ReadOnly Properties that could be used in DataBinding.

This is not a dynamic solution, with the bindable Properties being hard-coded at design-time, but it allows Master/Details binding, using a ComboBox and several Labels.

The wrapper Class

Imports System.Management
 
Public Class  BindableManagementObject
 
    Private mo As ManagementObject
 
    Public Sub  New(ByVal m As ManagementObject)
        Me.mo = m
    End Sub
 
    Public ReadOnly  Property Description() As String
        Get
            Return CStr(mo("Description"))
        End Get
    End Property
 
    Public ReadOnly  Property DisplayName() As String
        Get
            Return CStr(mo("DisplayName"))
        End Get
    End Property
 
    Public ReadOnly  Property Name() As String
        Get
            Return CStr(mo("Name"))
        End Get
    End Property
 
    Public ReadOnly  Property PathName() As String
        Get
            Return CStr(mo("PathName"))
        End Get
    End Property
 
    Public ReadOnly  Property State() As String
        Get
            Return CStr(mo("State"))
        End Get
    End Property
 
End Class

 

The binding code

Imports System.Management
 
Public Class  Form1
 
    Private Sub  Form1_Load(ByVal  sender As  System.Object, ByVal  e As  System.EventArgs) Handles MyBase.Load
        Me.MaximumSize = New  Size(1920, Me.Height)
        Dim searcher As New  ManagementObjectSearcher("Root\CIMV2", "SELECT * FROM Win32_Service")
        Dim queryobj() As BindableManagementObject = Array.ConvertAll(searcher.Get().Cast(Of ManagementObject).ToArray, Function(m) New  BindableManagementObject(m))
        comboDisplayName.DataSource = queryobj
        comboDisplayName.DisplayMember = "DisplayName"
        lblName.DataBindings.Add("Text", queryobj, "Name")
        lblDescription.DataBindings.Add("Text", queryobj, "Description")
        lblPathName.DataBindings.Add("Text", queryobj, "PathName")
        lblState.DataBindings.Add("Text", queryobj, "State")
    End Sub
 
End Class

 

Conclusion

Databinding is always a preferable solution when dealing with query data. A WMI query presents a great opportunity for Databinding, which can do a lot with minimal, concise, and clean code.

 

Download

Download here...

Other Resources