How to plug unplug USB devices
-
Hi all, Hope my title explains the all. I want to control the plug-unplug USB devices programatically, rather manually doing this. Found lots of articles on how to unplug, not other way round. Anybody of you have an idea about it. Thanks
I appreciate your help all the time... CodingLover :)
-
Hi all, Hope my title explains the all. I want to control the plug-unplug USB devices programatically, rather manually doing this. Found lots of articles on how to unplug, not other way round. Anybody of you have an idea about it. Thanks
I appreciate your help all the time... CodingLover :)
-
Hi all, Hope my title explains the all. I want to control the plug-unplug USB devices programatically, rather manually doing this. Found lots of articles on how to unplug, not other way round. Anybody of you have an idea about it. Thanks
I appreciate your help all the time... CodingLover :)
You can use WMI queries to check USB device is plugged or unplugged. void StartEventWatcher() { try { WqlEventQuery query = new WqlEventQuery("SELECT * FROM Win32_DeviceChangeEvent"); watcher = new ManagementEventWatcher(query); Console.WriteLine("Waiting for an event..."); watcher.EventArrived += new EventArrivedEventHandler(HandleDeviceChangeEvent); // Start listening for events watcher.Start(); } catch (ManagementException err) { MessageBox.Show("An error occurred while trying to receive an event: " + err.Message); } } private void HandleDeviceChangeEvent(object sender, EventArrivedEventArgs e) { IsDeviceConnected() } Boolean IsDeviceConnected() { ManagementObjectSearcher searcher = new ManagementObjectSearcher("root\\CIMV2", "SELECT * FROM Win32_USBDevice"); foreach (ManagementObject queryObj in searcher.Get()) { if (queryObj["Description"].ToString() == "Write the name of USB device";) { return true; } } return false; } WMI is use full , It we can directly write query to OS , to get required information form OS. Regards, Somnath Avhad.
-
You can use WMI queries to check USB device is plugged or unplugged. void StartEventWatcher() { try { WqlEventQuery query = new WqlEventQuery("SELECT * FROM Win32_DeviceChangeEvent"); watcher = new ManagementEventWatcher(query); Console.WriteLine("Waiting for an event..."); watcher.EventArrived += new EventArrivedEventHandler(HandleDeviceChangeEvent); // Start listening for events watcher.Start(); } catch (ManagementException err) { MessageBox.Show("An error occurred while trying to receive an event: " + err.Message); } } private void HandleDeviceChangeEvent(object sender, EventArrivedEventArgs e) { IsDeviceConnected() } Boolean IsDeviceConnected() { ManagementObjectSearcher searcher = new ManagementObjectSearcher("root\\CIMV2", "SELECT * FROM Win32_USBDevice"); foreach (ManagementObject queryObj in searcher.Get()) { if (queryObj["Description"].ToString() == "Write the name of USB device";) { return true; } } return false; } WMI is use full , It we can directly write query to OS , to get required information form OS. Regards, Somnath Avhad.
Member 8064748 wrote:
WMI is use full , It we can directly write query to OS , to get required information form OS.
True, I can get all the plug-unplug sate. But I cannot unplug a device with WMI.
I appreciate your help all the time... CodingLover :)
-
Hi all, Hope my title explains the all. I want to control the plug-unplug USB devices programatically, rather manually doing this. Found lots of articles on how to unplug, not other way round. Anybody of you have an idea about it. Thanks
I appreciate your help all the time... CodingLover :)
What does a device do when it is programmatically unplugged? Some devices switch off - how could you plug it programmatically? Some devices connect in a different mode to receive electricity via the USB port for charging their internal batteries (e.g. connect a "HID keyboard"). In such cases, the SDK of the manufacturer could provide a function to switch the device to its "normal" connected mode.
-
Hi all, Hope my title explains the all. I want to control the plug-unplug USB devices programatically, rather manually doing this. Found lots of articles on how to unplug, not other way round. Anybody of you have an idea about it. Thanks
I appreciate your help all the time... CodingLover :)
-
You can use WMI queries to check USB device is plugged or unplugged. void StartEventWatcher() { try { WqlEventQuery query = new WqlEventQuery("SELECT * FROM Win32_DeviceChangeEvent"); watcher = new ManagementEventWatcher(query); Console.WriteLine("Waiting for an event..."); watcher.EventArrived += new EventArrivedEventHandler(HandleDeviceChangeEvent); // Start listening for events watcher.Start(); } catch (ManagementException err) { MessageBox.Show("An error occurred while trying to receive an event: " + err.Message); } } private void HandleDeviceChangeEvent(object sender, EventArrivedEventArgs e) { IsDeviceConnected() } Boolean IsDeviceConnected() { ManagementObjectSearcher searcher = new ManagementObjectSearcher("root\\CIMV2", "SELECT * FROM Win32_USBDevice"); foreach (ManagementObject queryObj in searcher.Get()) { if (queryObj["Description"].ToString() == "Write the name of USB device";) { return true; } } return false; } WMI is use full , It we can directly write query to OS , to get required information form OS. Regards, Somnath Avhad.
Always wrap your code in Pre tag.