WMI - Win32_DeviceChangeEvent - Help


I am trying to detect when a device is plugged in to removed from my computer

Start Code------------------------------
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        q = New WqlEventQuery("Select * from Win32_DeviceChangeEvent")

        w = New ManagementEventWatcher(q)


    End Sub

    Private Sub w_EventArrived(ByVal sender As Object, ByVal e As System.Management.EventArrivedEventArgs) Handles w.EventArrived

        MessageBox.Show("New Arrival")

    End Sub

End Code---------------------------------

According to this site: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/wmisdk/wmi/win32_devicechangeevent.asp, Device Arrival has a value of 2 and Device Removal has a value of 3!

How do I use this information to display a different message box for value? e.g.

When the value is 2 (Device Arrival) do messagebox1.show("New Device Detected")
When the value is 3 (Device Removal) do messagebox1.show("Device Removed")

Anyone know how to do this?

Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Here is a similar example that detects a new USB-based disk drive...  

Imports System.Management

    Private WithEvents m_MediaConnectWatcher As ManagementEventWatcher

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        ' __InstanceOperationEvent will trap both Creation and Deletion of class instances
        Dim query2 As String = "SELECT * FROM __InstanceOperationEvent WITHIN 10 WHERE TargetInstance ISA ""Win32_DiskDrive"""
        m_MediaConnectWatcher = New ManagementEventWatcher(query2)
    End Sub

    Private Sub Arrived(ByVal sender As Object, ByVal e As System.Management.EventArrivedEventArgs) Handles m_MediaConnectWatcher.EventArrived
        Dim mbo, obj As ManagementBaseObject

        ' the first thing we have to do is figure out if this is a creation or deletion event
        mbo = CType(e.NewEvent, ManagementBaseObject)
        ' next we need a copy of the instance that was either created or deleted
        obj = CType(mbo("TargetInstance"), ManagementBaseObject)

        Select Case mbo.ClassPath.ClassName
            Case "__InstanceCreationEvent"
                If obj("InterfaceType") = "USB" Then
                    MsgBox(obj("Caption") & " has been plugged in")
                End If
            Case "__InstanceDeletionEvent"
                If obj("InterfaceType") = "USB" Then
                    MsgBox(obj("Caption") & " has been unplugged")
                End If
        End Select

    End Sub

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.