Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. How to plug unplug USB devices

How to plug unplug USB devices

Scheduled Pinned Locked Moved C#
helptutorial
7 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    CodingLover
    wrote on last edited by
    #1

    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 :)

    L S B R 4 Replies Last reply
    0
    • C 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 :)

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      I believe that the only way to plug back a device after it has been programatically unplugged is to physically remove the USB device and plug it back again.

      1 Reply Last reply
      0
      • C 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 :)

        S Offline
        S Offline
        Somnath T Avhad
        wrote on last edited by
        #3

        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.

        C R 2 Replies Last reply
        0
        • S Somnath T 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.

          C Offline
          C Offline
          CodingLover
          wrote on last edited by
          #4

          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 :)

          1 Reply Last reply
          0
          • C 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 :)

            B Offline
            B Offline
            Bernhard Hiller
            wrote on last edited by
            #5

            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.

            1 Reply Last reply
            0
            • C 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 :)

              R Offline
              R Offline
              RaviRanjanKr
              wrote on last edited by
              #6

              Try Eject USB disks using C#[^] Working with USB devices in .NET and C#[^] Detecting USB Drive Removal in a C# Program[^]

              1 Reply Last reply
              0
              • S Somnath T 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.

                R Offline
                R Offline
                RaviRanjanKr
                wrote on last edited by
                #7

                Always wrap your code in Pre tag.

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups