Skip to content

Instantly share code, notes, and snippets.

@tmontney
Created January 9, 2019 16:54
Show Gist options
  • Save tmontney/58bab43ecbabc7457057474762c0c052 to your computer and use it in GitHub Desktop.
Save tmontney/58bab43ecbabc7457057474762c0c052 to your computer and use it in GitHub Desktop.
Monitoring USB drive insertion
Private Sub weq_DeviceInsertion(sender As Object, e As EventArrivedEventArgs)
Dim LogDisk = e.NewEvent("TargetInstance")
Console.WriteLine(LogDisk.Properties("Name").Value)
End Sub
Private Sub AddWatcher()
'Copy weq_Insertion but use __InstanceDeletionEvent instead for Removals
Dim weq_Insertion As WqlEventQuery = New WqlEventQuery("SELECT * FROM __InstanceCreationEvent WITHIN 5 WHERE TargetInstance ISA 'Win32_LogicalDisk'")
w_Insertion = New ManagementEventWatcher(weq_Insertion)
AddHandler w_Insertion.EventArrived, AddressOf weq_DeviceInsertion
w_Insertion.Start()
End Sub
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment