Home > Net > C # monitor which have the external U disk inserted into the USB port
C # monitor which have the external U disk inserted into the USB port
Time:10-09
I want to write a piece of code in c #, which is need to monitor computer USB interface with external equipment or insert U disk, because they don't quite understand, so want to ask you, the following code I was looking for on the Internet, I'm not so sure function isn't all get on a computer USB interface corresponding ID name, if it is, in the code, please said USB port which is a unique identifier: the DeviceID or PNPDeviceID? If you have any access to judge which one is the interface, if the corresponding state of the interface has a property? Also please the great god, thank you very much
Public class Device { String deviceID; String pnpDeviceID; A string description;
Public Device (string deviceID, string pnpDeviceID, string description) { Enclosing deviceID=deviceID; Enclosing pnpDeviceID=pnpDeviceID; This. The description=the description; } }
//device USB port information The static List GetUSBDevice () { List Devices=new List (a); ManagementObjectCollection collection; Using (var a searcher=new ManagementObjectSearcher (@ "Select * From Win32_USBHub")) { The collection=a searcher. The Get (); }
The foreach (var device in the collection) { Device d=new Device ((string) Device. The GetPropertyValue (" DeviceID "), (string) device. The GetPropertyValue (" PNPDeviceID "), (string) device. The GetPropertyValue (" Description "));
Devices. The Add (d); }
Collection. The Dispose (); Return devices; }
CodePudding user response:
Have a look at the solution, and I also can't DingTie
CodePudding user response:
USB connection is a Windows message Can be received after you override WndProc public const int WM_DEVICECHANGE=0 x219;
Refer to https://blog.csdn.net/z0582/article/details/7328290