Question

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.

Was it helpful?

Solution

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);
                }
            }
        }

OTHER TIPS

You can look into WMI to get the hardware information.

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top