Reputation: 9
I would to know if it is possible to read when a device is connected to the pc (is not important if it is a usb storage or device or something else). I tried a bit, but I did not find anything. Someone can tell me how to do that? I would like to create something that understands when a device is inserted (maybe with a loop in background) and in case it is inserted it performs a specific action. I'm using VisualBasic .NET 4.5.2 (i can use what .NET version i want).
Upvotes: 0
Views: 740
Reputation: 4156
You can use the WMI to raise an event when a storage device is plugged into the computer
'set a reference to system management
Imports System.Management
Public Class Form1
Private WithEvents w As ManagementEventWatcher
Private q As WqlEventQuery
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("Device Event", e.Context.ToString)
End Sub
Private Sub Form1_Closing(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles MyBase.Closing
w.Stop()
End Sub
End Class
http://vb-tips.com/DeviceNotifyWMI.aspx
Upvotes: 1