public static List <EnumerateInfo> EnumerateDevicesWmi() { List <EnumerateInfo> enumeratedInfoList = new List <EnumerateInfo>(); try { using (ManagementObjectSearcher objSearcher = new ManagementObjectSearcher("Select * from Win32_PnPSignedDriver")) { using (ManagementObjectCollection objCollection = objSearcher.Get()) { foreach (ManagementObject objDriver in objCollection) { try { EnumerateInfo foundDevice = new EnumerateInfo(); enumeratedInfoList.Add(foundDevice); } catch { } } } } } catch (Exception ex) { Debug.WriteLine("Failed enumerating devices wmi: " + ex.Message); } return(enumeratedInfoList); }
public static List <EnumerateInfo> EnumerateDevicesDi(Guid enumerateGuid, bool isPresent) { IntPtr deviceInfoList = IntPtr.Zero; List <EnumerateInfo> enumeratedInfoList = new List <EnumerateInfo>(); try { int deviceIndexInfo = 0; SP_DEVICE_INFO_DATA deviceInfoData = new SP_DEVICE_INFO_DATA(); deviceInfoData.cbSize = Marshal.SizeOf(deviceInfoData); //Get device information if (isPresent) { deviceInfoList = SetupDiGetClassDevs(enumerateGuid, null, IntPtr.Zero, DiGetClassFlag.DIGCF_PRESENT | DiGetClassFlag.DIGCF_DEVICEINTERFACE); } else { deviceInfoList = SetupDiGetClassDevs(enumerateGuid, null, IntPtr.Zero, DiGetClassFlag.DIGCF_DEVICEINTERFACE); } while (SetupDiEnumDeviceInfo(deviceInfoList, deviceIndexInfo, ref deviceInfoData)) { try { deviceIndexInfo++; int deviceIndexInterfaces = 0; SP_DEVICE_INTERFACE_DATA deviceInterfaceData = new SP_DEVICE_INTERFACE_DATA(); deviceInterfaceData.cbSize = Marshal.SizeOf(deviceInterfaceData); while (SetupDiEnumDeviceInterfaces(deviceInfoList, deviceInfoData, enumerateGuid, deviceIndexInterfaces, ref deviceInterfaceData)) { try { deviceIndexInterfaces++; string devicePath = GetDevicePath(deviceInfoList, deviceInterfaceData); string description = GetBusReportedDeviceDescription(deviceInfoList, ref deviceInfoData); if (string.IsNullOrWhiteSpace(description)) { description = GetDeviceDescription(deviceInfoList, ref deviceInfoData); } string hardwareId = GetDeviceHardwareId(deviceInfoList, ref deviceInfoData); string modelId = GetDeviceModelId(devicePath); EnumerateInfo foundDevice = new EnumerateInfo(); foundDevice.DevicePath = devicePath; foundDevice.Description = description; foundDevice.HardwareId = hardwareId; foundDevice.ModelId = modelId; enumeratedInfoList.Add(foundDevice); } catch { } } } catch { } } } catch (Exception ex) { Debug.WriteLine("Failed enumerating devices di: " + ex.Message); } finally { if (deviceInfoList != IntPtr.Zero) { SetupDiDestroyDeviceInfoList(deviceInfoList); } } return(enumeratedInfoList); }