private void CheckForDevices(object sender, EventArgs e) { try { var regex = new Regex(_DeviceIDRegex); using (var set = new DeviceInformationSet()) { var devices = set.GetAllDevices().Where(d => regex.IsMatch(d.HardwareID)).ToArray(); if (devices.Length == 1) { Close(); } } } catch { } }
private DeviceInterface FindDevice() { Guid hidGuid = DeviceInformationSet.GetHidGuid(); using (var deviceInformationSet = new DeviceInformationSet(hidGuid, DiGetClassFlags.Present | DiGetClassFlags.DeviceInterface)) { DeviceInterface deviceInterface = deviceInformationSet.GetDeviceInterfaces(hidGuid) .Where(d => d.IsValidUsbDevice) .FirstOrDefault(dis => dis.Details.DevicePath.Contains(VendorId) && dis.Details.DevicePath.Contains(ProductId)); if (deviceInterface == null) { _log.Debug("device not found"); } else { _log.Debug("device found: " + deviceInterface.Details.DevicePath); } return(deviceInterface); } }
private bool TryOpen(int blockSize) { Dispose(); DeviceInterface deviceInterface; Guid hidGuid = DeviceInformationSet.GetHidGuid(); using (var deviceInformationSet = new DeviceInformationSet(hidGuid, DiGetClassFlags.Present | DiGetClassFlags.DeviceInterface)) { deviceInterface = deviceInformationSet.GetDeviceInterfaces(hidGuid) .Where(d => d.IsValidUsbDevice) .FirstOrDefault(dis => (dis.Details.DevicePath.Contains("16c0") && dis.Details.DevicePath.Contains("0478")) || (dis.Details.DevicePath.Contains("03eb") && dis.Details.DevicePath.Contains("2067"))); if (deviceInterface != null) { _handle = deviceInterface.OpenFile(blockSize + 3); return(true); } return(false); } }
private void FixDriversIfNeeded() { var regex = new Regex(DeviceConnectionRequestWindow.GetDeviceIDRegex()); var paragraph = new Paragraph(); paragraph.Inlines.Add(new Run("Checking whether the driver needs updating...\r\n") { Foreground = Brushes.DarkBlue }); txtLog.Document.Blocks.Add(paragraph); for (int pass = 0; pass < 2; pass++) { using (var set = new DeviceInformationSet()) { var devices = set.GetAllDevices().Where(d => regex.IsMatch(d.HardwareID)).ToArray(); if (devices.Length != 1) { return; } var device = devices[0]; paragraph.Inlines.Add(new Run($"Found {device.DeviceID}...\r\n") { Foreground = Brushes.DarkBlue }); if (device.UserFriendlyName == "STM32 Bootloader") { return; } Controller.StatusText = "Installing STM32 bootloader drivers..."; paragraph.Inlines.Add(new Run($"Bootloader driver not installed for {device.UserFriendlyName}\r\n") { Foreground = Brushes.DarkBlue }); var driver = set.GetCompatibleDrivers(device).FirstOrDefault(d => d.Description == "STM32 Bootloader"); if (driver.Description == null) { if (pass == 0) { string driverDir = System.IO.Path.Combine(Controller.DataDirectory, "Driver"); paragraph.Inlines.Add(new Run($"Copying drivers from {driverDir}...\r\n") { Foreground = Brushes.DarkBlue }); if (!DeviceInformationSet.SetupCopyOEMInf(System.IO.Path.Combine(driverDir, "STM32Bootloader.inf"), driverDir, DeviceInformationSet.OemSourceMediaType.SPOST_PATH, DeviceInformationSet.OemCopyStyle.Default, null, 0, IntPtr.Zero, null)) { throw new LastWin32ErrorException("Failed to install the STM32 Bootloader driver"); } continue; } else { throw new Exception("STM32 Bootloader Driver Not Found"); } } paragraph.Inlines.Add(new Run($"Installing the STM32 bootloader driver...\r\n") { Foreground = Brushes.DarkBlue }); set.InstallSpecificDriverForDevice(device, driver, IntPtr.Zero); } } }
public static IEnumerable <DeviceInterface> GetCdDrives() { var devInfo = new DeviceInformationSet(CdDriveGuid, DeviceInformationClassFlags.Present | DeviceInformationClassFlags.DeviceInterface); return(devInfo.GetDevices()); }