Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
371 views
Welcome To Ask or Share your Answers For Others

1 Answer

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("" + descriptor);
                try
                {
                    using (Device device = descriptor.OpenDevice())
                    {
                        Console.WriteLine("     Product: " + device.Product);
                        Console.WriteLine("Manufacturer: " + device.Manufacturer);
                        Console.WriteLine();
                    }
                }
                catch (UsbException e)
                {
                    Console.WriteLine("Got Exception : " + e);
                }
            }
        }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...