/// <summary> /// Clean up any resources being used. /// </summary> /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param> protected override void Dispose(bool disposing) { if (disposing && (components != null)) { // Whenever the form closes we need to unregister the // hardware notifier. Failure to do so could cause // the system not to release some resources. Calling // this method if you are not currently hooking the // hardware events has no ill effects so better to be // safe than sorry. if (detector != null) { detector.Dispose(); detector = null; } components.Dispose(); } base.Dispose(disposing); }
private static string GetStringPropertyForDevice(IntPtr deviceInfoSet, SP_DEVINFO_DATA deviceInfoData, SPDRP propId) { uint proptype; uint outsize = 0; var buffer = new byte[MAX_DEVICE_LEN]; var result = DeviceDetector.SetupDiGetDeviceRegistryProperty(deviceInfoSet, ref deviceInfoData, (uint)propId, out proptype, buffer, (uint)buffer.Length, out outsize); if (!result) { var errcode = Marshal.GetLastWin32Error(); if (errcode == ERROR_INVALID_DATA) { return(null); } throw new Exception("Error calling SetupDiGetDeviceRegistryPropertyW: " + errcode); } var o = ""; if (outsize > 0) { switch (proptype) { case (uint)REG.REG_SZ: o = Encoding.Unicode.GetString(buffer, 0, (int)outsize - 2); break; case (uint)REG.REG_MULTI_SZ: o = Encoding.Unicode.GetString(buffer, 0, (int)outsize - 2); o = o.Trim('\0').Replace("\0", "\r\n"); break; default: break; } } return(o); }
public DeviceDetectorForm(DeviceDetector detector) { InitializeComponent(); _Detector = detector; _Detector.DeviceChanged += new DeviceDetector.DeviceDetectorEventHandler(_Detector_DeviceChanged); }
private async Task CheckAndClean(bool clean) { LogTextBox.Clear(); MainTabControl.SelectedTab = LogsTabPage; var cancellationToken = new CancellationToken(false); var so = ControlsHelper.MainTaskScheduler; var unused = Task.Factory.StartNew(() => { AddLogLine("Enumerating Devices..."); var devices = DeviceDetector.GetDevices(); var offline = devices.Where(x => !x.IsPresent && x.IsRemovable && !x.Description.Contains("RAS Async Adapter")).ToArray(); var problem = devices.Where(x => x.Status.HasFlag(DeviceNodeStatus.DN_HAS_PROBLEM)).Except(offline).ToArray(); var unknown = devices.Where(x => x.Description.Contains("Unknown")).Except(offline).Except(problem).ToArray(); var list = new List <string>(); if (offline.Length > 0) { list.Add(string.Format("{0} offline devices.", offline.Length)); } if (problem.Length > 0) { list.Add(string.Format("{0} problem devices.", problem.Length)); } if (unknown.Length > 0) { list.Add(string.Format("{0} unknown devices.", unknown.Length)); } var message = string.Join("\r\n", list); if (list.Count == 0) { AddLogLine("No offline, problem or unknown devices found."); } else if (clean) { foreach (var item in list) { AddLogLine(item); } var result = DialogResult.No; ControlsHelper.Invoke(new Action(() => { var form = new JocysCom.ClassLibrary.Controls.MessageBoxForm { StartPosition = FormStartPosition.CenterParent }; ControlsHelper.CheckTopMost(form); result = form.ShowForm( "Do you want to remove offline, problem or unknown devices?\r\n\r\n" + message, "Do you want to remove devices?", MessageBoxButtons.YesNo, MessageBoxIcon.Warning); form.Dispose(); })); if (result != DialogResult.Yes) { return; } var devList = new List <DeviceInfo>(); devList.AddRange(offline); devList.AddRange(problem); devList.AddRange(unknown); for (var i = 0; i < devList.Count; i++) { var item = devList[i]; AddLogLine("Removing Device: {0}/{1} - {2}", i + 1, list.Count, item.Description); try { var exception = DeviceDetector.RemoveDevice(item.DeviceId); if (exception != null) { AddLogLine(exception.Message); } //System.Windows.Forms.Application.DoEvents(); } catch (Exception ex) { AddLogLine(ex.Message); } } } AddLogLine("Done"); }, CancellationToken.None, TaskCreationOptions.LongRunning, so).ConfigureAwait(true); }
private void ScanButton_Click(object sender, EventArgs e) { DeviceDetector.ScanForHardwareChanges(); }
/// <summary> /// In the form load we take an initial hardware inventory, /// then hook the notifications so we can respond if any /// device is added or removed. /// </summary> private void HardwareControl_Load(object sender, EventArgs e) { if (IsDesignMode) return; UpdateButtons(); RefreshHardwareList(); detector = new DeviceDetector(false); detector.DeviceChanged += new DeviceDetector.DeviceDetectorEventHandler(detector_DeviceChanged); }