列舉 USB 裝置

USBDeviceNames = New List(Of String)
Dim moSearch, moSearchDevice As ManagementObjectSearcher
Dim mo, moDevice As ManagementObject
Dim strDevice As String = ""
Dim strDeviceName As String = ""
moSearch = New ManagementObjectSearcher _
("Select * from Win32_USBControllerDevice")
For Each mo In moSearch.Get
 strDeviceName = mo("Dependent").ToString.Replace(""""c, "")
 strDevice = strDeviceName.Substring(strDeviceName.IndexOf("=") + 1)
 If strDevice.Contains("USB") AndAlso 
  strDevice.Contains("ROOT_HUB") = False Then
  moSearchDevice = New ManagementObjectSearcher
                  ("Select * From Win32_PnPEntity _
                  Where DeviceID = '" & strDevice & "'")
  For Each moDevice In moSearchDevice.Get
   If CStr(moDevice.Properties("service").Value) <> "" Then
    Dim devStatus As String = CStr(moDevice("Status"))
    Dim devName As String = CStr(moDevice("Name"))
    If devStatus.ToUpper.Trim = "OK" And devName <> "" Then
     If USBDeviceNames.IndexOf(devName) = -1 Then
      USBDeviceNames.Add(devName)
     End If
    End If
   End If
  Next moDevice
 End If
 Application.DoEvents()
Next mo   

留言

熱門文章