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.