WMI - Win32_DeviceChangeEvent - Help

Hi

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)

        w.Start()

    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

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/wmisdk/wmi/win32_devicechangeevent.asp
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?

Thanks
LVL 1
narmi2Asked:
Who is Participating?
 
grayeCommented:
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)
        m_MediaConnectWatcher.Start()
    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
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.