Windows Develop Bookmark and Share   
 index > Windows Forms General > USB: plug-in, unplug events and to get detatils of connected device
 

USB: plug-in, unplug events and to get detatils of connected device

Hello,

I am usingManagementEventWatcher class to detect plug-in unplug events as shown below. The below code works fine.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Management;

namespace UsbDetect_Test
{
    class Program
    {
        static void Main(string[] args)
        {
            AddInsertUSBHandler();
            AddRemoveUSBHandler();
            while (true)
            {
            }

        }

         static ManagementEventWatcher w = null;

        static void AddRemoveUSBHandler()
        {

                WqlEventQuery q;
                ManagementScope scope = new ManagementScope("root\\CIMV2");
                scope.Options.EnablePrivileges = true;

                try {

                        q = new WqlEventQuery();
                        q.EventClassName = "__InstanceDeletionEvent";
                        q.WithinInterval = new TimeSpan(0, 0, 3);
                        q.Condition = "TargetInstance ISA 'Win32_USBControllerdevice'";
                        w = new ManagementEventWatcher(scope, q);
                        w.EventArrived += USBRemoved;

                        w.Start();
                }
                catch (Exception e) {


                        Console.WriteLine(e.Message);
                        if (w != null)
                        {
                                w.Stop();

                        }
                }

        }

        static void AddInsertUSBHandler()
        {

                WqlEventQuery q;
                ManagementScope scope = new ManagementScope("root\\CIMV2");
                scope.Options.EnablePrivileges = true;

                try {

                        q = new WqlEventQuery();
                        q.EventClassName = "__InstanceCreationEvent";
                        q.WithinInterval = new TimeSpan(0, 0, 3);
                        q.Condition = "TargetInstance ISA 'Win32_USBControllerdevice'";
                        w = new ManagementEventWatcher(scope, q);
                        w.EventArrived += USBInserted;
                        
                        w.Start();
                }
                catch (Exception e) {

                        Console.WriteLine(e.Message);
                        if (w != null)
                        {
                                w.Stop();

                        }
                }

        }

        static void USBInserted(object sender, EventArgs e)
        {

                Console.WriteLine("A USB device inserted");
               
        }

        static void USBRemoved(object sender, EventArgs e)
        {

                Console.WriteLine("A USB device removed");

        }
    }

    
}
And I have no problem with this code.
But I want to know the following when usb is plugged in
  1. port name to which the device is connected.
  2. Driver description
  3. Install ID
  4. Driver
How to do this?

With regards

its me
Watashi_me  Wednesday, September 02, 2009 2:40 AM
Hi Watashi_me,

The article below shows how to monitor USB devices and get their detail information:
http://huntjason.spaces.live.com/blog/cns!9D2E96F2AA6AE85F!156.entry?wa=wsignin1.0&sa=659828468.

This is a similar thread about how to enumerate USB devices:
http://social.msdn.microsoft.com/Forums/en-US/netfxbcl/thread/c0694939-0105-4c5e-a0ea-8c7c5e9dda47.

Regards,
Aland Li
Please mark the replies as answers if they help and unmark if they don't. This can be beneficial to other community members reading the thread.
Aland Li  Friday, September 04, 2009 3:31 AM
Hi Watashi_me,

Could you let me know if my reply helps.

Regards,
Aland Li
Please mark the replies as answers if they help and unmark if they don't. This can be beneficial to other community members reading the thread.
Aland Li  Wednesday, September 09, 2009 9:55 AM

Hi,

We are changing the issue type to “General Discussion�because you have not followed up with the necessary information. If you have more time to look at the issue and provide more information, please feel free to change the issue type back to “Question�by opening the Options list at the top of the post window, and changing the type. If the issue is resolved, we will appreciate it if you can share the solution so that the answer can be found and used by other community members having similar questions.

Aland Li


Please mark the replies as answers if they help and unmark if they don't. This can be beneficial to other community members reading the thread.
Aland Li  Thursday, September 10, 2009 3:26 AM

You can use google to search for other answers

Custom Search

More Threads

• How to get toolStrip transparent background?
• type Q??
• Clicking doesn't bring MDI windows to the front!
• MSDN like help interface
• OnScroll event for web browser?
• Hello, every one, wanner a suggestion
• UserControl Close, Exit, Terminate, Etc.
• Setting Custom Property on Windows Folders
• how to protect the screen??
• File Upload in windows form