Search code examples
vb.netsystemmanagement

(VB.NET) Is There a Way to Get a List of the Categories from Device Manager?


I am working in VB.NET and trying to automate a driver install process. I am stuck on finding a way (if even possible?) to check the device manager. The driver in question makes it's own section/category (For lack of a better term. Similar to the Batteries, Monitors, Processors, Ports, etc. level in the device manager list). It is proprietary, so if the "Custom Driver" section/category is listed I just want to have my WinForm show/hide some buttons if the driver is already installed.

I have made the reference to System.Management in project resources and added the namespace Imports System.Management to the top of my code but I cannot find an example that's even close to what I would like to do.

'Pseudo Code:
DIM name as String = "Custom Driver"
DIM DevMangr as New System.Management.Reader()
DIM Category as String = DevMangr.ReadLine()
Do While DevMangr.Peek <> -1
    If Category.StartsWith(name)
        Button1.Visible = False
        Label1.Visible = True
        Button2.Visible = True
        Label2.Visible = False
    End If
Loop
DevMan.Close()

Solution

  • Thanks to help from Jimi in the Comments, I was able to solve my problem. Actual code for anyone interested is below:

    Dim ConnOptions As New ConnectionOptions() With {.EnablePrivileges = True, .Timeout = EnumerationOptions.InfiniteTimeout}
    Dim mOptions As New EnumerationOptions() With {.Rewindable = False, .ReturnImmediately = True, .DirectRead = True, .EnumerateDeep = False}
    Dim mQuery As New SelectQuery("SELECT * FROM Win32_PnPEntity WHERE Name LIKE '%Custom Driver%'")
    Dim mScope As New ManagementScope($"\\{Environment.MachineName}\root\CIMV2", ConnOptions)
    mScope.Connect()
        Using moSearcher As New ManagementObjectSearcher(mScope, mQuery, mOptions)
            For Each Item As ManagementObject In moSearcher.[Get]()
            Next
            If moSearcher.Get.Count > 0 Then
                Label1.Visible = True
            End If
        End Using
    

    Essentially what this does is check Device Manager for "Custom Driver" and if it is there, turn Label1 on, which is a message about the driver being all set.