tags:

views:

326

answers:

2

Is there a way to detect when a specific device is plugged into a usb port, what I would like to happen is when I plug my laptop into my docking station it run up several apps to account for my different keyboard, mouse and monitors. Specifically I have an issue with some software for my G15 keyboard stopping media player closing properly.

Hopefully in .NET but if not any suggestions appreciated.

+2  A: 

Try using SharpUSBLib. It's a C# wrapper around the libusb project.

I'm pasting a code sample below (included in the download - just tried it myself). It seems simple enough and I think it will provide you with quite lot of info on devices connected to your laptop via USB.

  foreach (Bus bus in Bus.Busses)
        {
            Console.WriteLine(bus);
            foreach (Descriptor descriptor in bus.Descriptors)
            {
                Console.WriteLine("\t" + descriptor);
                try
                {
                    using (Device device = descriptor.OpenDevice())
                    {
                        Console.WriteLine("\t\t     Product: " + device.Product);
                        Console.WriteLine("\t\tManufacturer: " + device.Manufacturer);
                        Console.WriteLine();
                    }
                }
                catch (UsbException e)
                {
                    Console.WriteLine("Got Exception : " + e);
                }
            }
        }
adilei
What is missing is detecting the event of 'when a specific device is plugged into a usb port'. One could imagine keeping a dictionary of connected devices with this code on a timer, but not the answer I suspect.
kenny
+2  A: 

You can look into WMI to get the hardware information.

http://msdn.microsoft.com/en-us/library/aa394582.aspx

stiduck