Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
351 views
in Technique[技术] by (71.8m points)

.net - Start program on usb hardware plugin

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

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

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...