/// <summary> /// 在U盘移除后清理可移动磁盘列表和控件 /// </summary> private void ClearDriveList() { DriveInfo[] drives = DriveInfo.GetDrives(); //清除在DriveList中但已被移除的设备,通常是直接拔出U盘 foreach (USBDevice usbDrive in DriveList) { if (!Array.Exists(drives, x => x.Name == usbDrive.Name)) { leftPanel.Children.Remove(usbDrive.Button); rightPanel.Children.Remove(usbDrive.ComState); DriveList.Remove(usbDrive); break; } } //清除已移除但仍在DriveList中并未清除控件的设备,通常是使用界面按钮移除的设备 foreach (DriveInfo drive in drives) { if (!drive.IsReady) { USBDevice usbDrive = DriveList.Find(x => x.Name == drive.Name); if (usbDrive != null) { leftPanel.Children.Remove(usbDrive.Button); rightPanel.Children.Remove(usbDrive.ComState); DriveList.Remove(usbDrive); } } } if (DriveList.Count == 0) { NoDriveState.Visibility = Visibility.Visible; } }
void OnDriveChanged(object sender, object NewValue) { IDrive newDrive = driveList.Find(NewValue as string); schedule.DriveChanged(drive, newDrive, time, position); drive = newDrive; }
/// <summary> /// 弹出U盘 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void DoUSBout(object sender, RoutedEventArgs e) { var sourceButton = sender as Button; string deviceName = DriveList.Find(x => x.Button.Equals(sourceButton)).Name; uint removeState = USBDevice.RemoveDevice(deviceName); if (removeState == 0) { //MessageBox.Show(string.Format("可移动设备{0}已移除", deviceName)); ClearDriveList(); } else { MessageBox.Show(string.Format("可移动设备{0}移除失败代码:{1}", deviceName, removeState.ToString())); } }