notify usb device

kaveh rahimi 66 Reputation points
2021-05-01T08:04:19.67+00:00

Hi , I am writing a code to notify arrival or removal of usb device.I don't know which classes do I have use and what are the objects and events to notify that.Please help me.
Thanks

Developer technologies VB
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Xingyu Zhao-MSFT 5,381 Reputation points
    2021-05-03T02:44:09.457+00:00

    Hi @kaveh rahimi
    Here's an example of vb. net you can refer to(code from here).

        Public Enum EventType  
            Inserted = 2  
            Removed = 3  
        End Enum  
        Sub Main()  
            Dim watcher As ManagementEventWatcher = New ManagementEventWatcher()  
            Dim query As WqlEventQuery = New WqlEventQuery("SELECT * FROM Win32_VolumeChangeEvent WHERE EventType = 2 or EventType = 3")  
            AddHandler watcher.EventArrived, AddressOf watcher_EventArrived  
            watcher.Query = query  
            watcher.Start()  
            Console.ReadKey()  
        End Sub  
      
        Private Sub watcher_EventArrived(sender As Object, e As EventArrivedEventArgs)  
            Dim driveName As String = e.NewEvent.Properties("DriveName").Value.ToString()  
            Dim eventType As EventType = CType((Convert.ToInt16(e.NewEvent.Properties("EventType").Value)), EventType)  
            Dim eventName As String = [Enum].GetName(GetType(EventType), eventType)  
            Console.WriteLine("{0}: {1} {2}", DateTime.Now, driveName, eventName)  
        End Sub  
    

    Hope it could be helpful.

    Best Regards,
    Xingyu Zhao
    *
    If the answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


  2. Mia Wu-MSFT 327 Reputation points Microsoft Employee Moderator
    2021-05-25T05:32:15.95+00:00

    Could you please check if this article solve your problem?


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.