Results 1 to 3 of 3

Thread: USB devicename

  1. #1

    Thread Starter
    New Member
    Join Date
    Apr 2013
    Posts
    7

    USB devicename

    Hi,

    there are many examples of how to list the USB devices connected to a system and also code to say when a USB device has been connected and disconnected. I am trying to modify this code to display the friendly device name, but without any success:

    If e.NewEvent.ClassPath.ClassName = "__InstanceCreationEvent" Then
    s += e.NewEvent.Properties("TargetInstance").Value.Properties("Antecedent").Value.ToString() + ControlChars.CrLf
    s += e.NewEvent.Properties("TargetInstance").Value.Properties("Dependent").Value.ToString() + ControlChars.CrLf
    MsgBox("USB inserted, the information is " + ControlChars.CrLf + s)

    ElseIf e.NewEvent.ClassPath.ClassName = "__InstanceDeletionEvent" Then
    s += e.NewEvent.Properties("TargetInstance").Value.Properties("Antecedent").Value.ToString() + ControlChars.CrLf
    s += e.NewEvent.Properties("TargetInstance").Value.Properties("Dependent").Value.ToString() + ControlChars.CrLf
    MsgBox("USB removed, the information is " + s)
    Else
    MsgBox(e.NewEvent.ClassPath.ClassName)
    End If

    Can I extract the friendly device name from the above code?

    Many thanks

    Lawrence

  2. #2
    Frenzied Member
    Join Date
    Oct 2012
    Location
    Tampa, FL
    Posts
    1,187

    Re: USB devicename

    this might interest you:

    Code:
    Imports System
    Imports System.Collections.Generic
    Imports System.Management
    Namespace ConsoleApplication1
    	' need to add System.Management to your project references.
    	Class Program
    		Private Shared Sub Main(args As String())
    			Dim usbDevices = GetUSBDevices()
    
    			For Each usbDevice As var In usbDevices
    				Console.WriteLine("Device ID: {0}, PNP Device ID: {1}, Description: {2}", usbDevice.DeviceID, usbDevice.PnpDeviceID, usbDevice.Description)
    			Next
    
    			Console.Read()
    		End Sub
    
    		Private Shared Function GetUSBDevices() As List(Of USBDeviceInfo)
    			Dim devices As New List(Of USBDeviceInfo)()
    
    			Dim collection As ManagementObjectCollection
    			Using searcher = New ManagementObjectSearcher("Select * From Win32_USBHub")
    				collection = searcher.[Get]()
    			End Using
    
    			For Each device As var In collection
    				devices.Add(New USBDeviceInfo(DirectCast(device.GetPropertyValue("DeviceID"), String), DirectCast(device.GetPropertyValue("PNPDeviceID"), String), DirectCast(device.GetPropertyValue("Description"), String)))
    			Next
    
    			collection.Dispose()
    			Return devices
    		End Function
    	End Class
    
    	Class USBDeviceInfo
    		Public Sub New(deviceID As String, pnpDeviceID As String, description As String)
    			Me.DeviceID = deviceID
    			Me.PnpDeviceID = pnpDeviceID
    			Me.Description = description
    		End Sub
    		Public Property DeviceID() As String
    			Get
    				Return m_DeviceID
    			End Get
    			Private Set
    				m_DeviceID = Value
    			End Set
    		End Property
    		Private m_DeviceID As String
    		Public Property PnpDeviceID() As String
    			Get
    				Return m_PnpDeviceID
    			End Get
    			Private Set
    				m_PnpDeviceID = Value
    			End Set
    		End Property
    		Private m_PnpDeviceID As String
    		Public Property Description() As String
    			Get
    				Return m_Description
    			End Get
    			Private Set
    				m_Description = Value
    			End Set
    		End Property
    		Private m_Description As String
    	End Class
    End Namespace
    Found originally herE:
    http://stackoverflow.com/questions/3...ed-usb-devices

  3. #3

    Thread Starter
    New Member
    Join Date
    Apr 2013
    Posts
    7

    Re: USB devicename

    Hi jayinthe813

    sorry about the delay. Thanks for the code.

    The original plan was to use the code in my Post which says there has been a USB device inserted or removed and try to figure out the device name. I have already played around with code similar to the code in your reply but it takes a noticeable amount of time. I wonder if it is possible to find out the device name by changing the original code? Windows seems to be able to do it.

    Many thanks in advance,

    Lawrence

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width