private void RemoveButton_Click(object sender, EventArgs e) { var row = DeviceDataGridView.SelectedRows.Cast <DataGridViewRow>().FirstOrDefault(); if (row != null) { var device = (DeviceInfo)row.DataBoundItem; if (device.IsRemovable) { DeviceDetector.RemoveDevice(device.DeviceId); } } }
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); }