Click here to Skip to main content
15,887,898 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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...


C#
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.
Posted
Updated 8-Sep-15 19:55pm
v2
Comments
Patrice T 9-Sep-15 1:53am    
And what your problem ?
Ajith.Kerala 9-Sep-15 1:58am    
USB thumb drives
USB harddisks
USB DVD writer
USB Bluetooth devices
USB headsets
usb mouse
USB keyboard
USB webcams / cameras ,these device also want to detect
Patrice T 9-Sep-15 2:00am    
This is not a question !
Ajith.Kerala 9-Sep-15 2:02am    
i need a solution for this, searched lot but i didnt get, if you can please help
sreeyush sudhakaran 9-Sep-15 2:39am    
Have a look at
http://stackoverflow.com/questions/24944865/monitor-usb-drives-and-retrieve-device-info-using-a-devicewatcher
http://blogs.msdn.com/b/powershell/archive/2007/02/24/displaying-usb-devices-using-wmi.aspx

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900