USB thumb drives
USB harddisks
USB DVD writer
USB Bluetooth devices
USB headsets
usb mouse
USB keyboard
USB webcams / cameras
Just want to detect any sort of usb device using event handler... Would appreciate any help...
WqlEventQuery q_creation = new WqlEventQuery();
private void Form2_Load(object sender, EventArgs e)
{
q_creation.EventClassName = "__InstanceCreationEvent";
q_creation.WithinInterval = new TimeSpan(0, 0, 2); //How often do you want to check it? 2Sec.
q_creation.Condition = @"TargetInstance ISA 'Win32_DiskDriveToDiskPartition'";
var mwe_creation = new ManagementEventWatcher(q_creation);
mwe_creation.EventArrived += new EventArrivedEventHandler(USBEventArrived_Creation);
mwe_creation.Start(); // Start listen for events
}
/// <summary>
/// Callback event
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
internal void USBEventArrived_Creation(object sender, EventArrivedEventArgs e)
{
MessageBox.Show("Device Connected");
}
This is the code what i had tried.
I am pasting my entire class for you, use as is (hookup the events right at the bottom for detecting or removal of USB) :