Register insertion of USB drives in .NET 1.1

  • Thread starter Thread starter Paul Taylor
  • Start date Start date
P

Paul Taylor

Hi there,

I'm trying to write something in VB.NET that registers the insertion of a
USB device (specifically a USB flash drive) and acts upon the event. I've
done some reading up and it seems that using ManagementEventWatchers is the
way to go, yet using the code below in a test project, I find that inserting
a USB drive triggers up to about 5 events instead of just the 1 I'm
expecting.

If anyone could point out the error of my ways that'd be great.

Cheers

Paul Taylor


Imports System.management
Public Class Form1
Inherits System.Windows.Forms.Form
...
Dim query As WqlEventQuery
Dim WithEvents watch As ManagementEventWatcher
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
query = New WqlEventQuery("Select * from Win32_DeviceChangeEvent")
watch = New ManagementEventWatcher(query)
watch.Start()
End Sub

Private Sub w_EventArrived(ByVal sender As Object, ByVal e As
System.Management.EventArrivedEventArgs) Handles watch.EventArrived
MsgBox("Event!")
End Sub
End Class
 
Back
Top