Home All Groups Group Topic Archive Search About

Register insertion of USB drives in .NET 1.1

Author
1 Feb 2006 4:20 AM
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

Author
1 Feb 2006 2:41 PM
Cowboy (Gregory A. Beamer) - MVP
This might help:
http://www.developerfusion.co.uk/show/4392/

--
Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA

***************************
Think Outside the Box!
***************************


Show quote
"Paul Taylor" wrote:

> 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
>
>
>

AddThis Social Bookmark Button