/// <summary> /// Rerurns a list of DeviceInfo for all USB Pnp devices. /// </summary> /// <returns></returns> public List <DeviceInfo> GetAll() { try { List <DeviceInfo> allDevices = new(); ManagementObjectCollection devices; ManagementScope scope = new ManagementScope(@"\\" + Environment.MachineName + @"\root\CIMV2"); using (var searcher = new ManagementObjectSearcher(@"Select * From Win32_PnPEntity")) devices = searcher.Get(); foreach (var device in devices) { if (device.Properties["Name"].Value != null && device.Properties["Name"].Value.ToString().StartsWith("USB")) { allDevices.Add(DeviceInfoTools.CreateDeviceInfo(device)); } } devices.Dispose(); return(allDevices); } catch (Exception ex) { throw ex; } }
/// <summary> /// creates an USBDetectionEventArgs object with available information /// about the detected USB device. /// </summary> /// <param name="eventType">Kund of action detected (inserted, removed).</param> /// <param name="e">nformation about the arrived event.</param> /// <returns></returns> USBDetectionEventArgs CreateEventArg(USBDetectionEventType eventType, EventArrivedEventArgs e) { //Todo: Can more information be added from the EventArrivedEventArgs? ManagementBaseObject instance = (ManagementBaseObject)e.NewEvent["TargetInstance"]; var deviceInfo = DeviceInfoTools.CreateDeviceInfo(instance, e.NewEvent); return(new USBDetectionEventArgs { Action = eventType, DeviceInfo = deviceInfo }); }
/// <summary> /// Rerurns a list of DeviceInfo for all USB HUB devices. /// </summary> /// <returns></returns> public List <DeviceInfo> GetAll() { try { List <DeviceInfo> allDevices = new(); ManagementObjectCollection devices; using (var searcher = new ManagementObjectSearcher(@"Select * From Win32_USBHub")) devices = searcher.Get(); foreach (var device in devices) { allDevices.Add(DeviceInfoTools.CreateDeviceInfo(device)); } devices.Dispose(); return(allDevices); } catch (Exception ex) { throw ex; } }