/// <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; } }
/// <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())); } }
/// <summary> /// 增加设备,用于在设备插入后将其添加到列表和处理队列中 /// </summary> private void AddUSBDevices() { var drives = DriveInfo.GetDrives(); string deviceNum = ""; foreach (var drive in drives) { if (drive.IsReady && drive.DriveType == DriveType.Removable && !DriveList.Exists(x => x.Name == drive.Name) && Directory.Exists(drive.Name + MainWindow.devicePath)) { try { deviceNum = drive.Name; if (File.Exists(drive.Name + "Config.ini")) { string[] deviceInfo = File.ReadAllLines(drive.Name + "Config.ini", Encoding.Default); if (deviceInfo.Length > 1 && deviceInfo[0] == "调车") { deviceNum = deviceInfo[1] + "号"; } } string[] files = Directory.GetFiles(drive.Name + MainWindow.devicePath, "YC?????_*.MP4", SearchOption.TopDirectoryOnly); if (files.Length != 0) { deviceNum = System.IO.Path.GetFileName(files[files.GetUpperBound(0)]).Substring(6, 1) + "号"; } } catch { MessageBox.Show("部分U盘设备未连接好,请重新连接"); } AddControl(deviceNum, out ComState comState, out Button button); USBDevice usbDevice = new USBDevice(drive.Name, comState, button, drive.DriveType); DriveList.Add(usbDevice); CopyQueue.Enqueue(usbDevice); } } if (DriveList.Count == 0) { NoDriveState.Visibility = Visibility.Visible; } }
/// <summary> /// 任务完成事件 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void Task_Completed(object sender, RunWorkerCompletedEventArgs e) { this.Dispatcher.Invoke(new Action(() => { USBDevice device = CopyQueue.Dequeue(); device.Button.IsEnabled = true; if (device.ComState.State != "Nothing") { device.ComState.StateCompete(); } else { device.ComState.StateNothing(); } })); if (CopyQueue.Count != 0) { Start(); } else { isProcessing = UnProcessing; } }
/// <summary> /// 执行任务事件 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void DoTask(object sender, DoWorkEventArgs e) { isProcessing = Processing; USBDevice drive = CopyQueue.Peek(); try { string[] files = Directory.GetFiles(drive.Name + MainWindow.devicePath, "YC?????_*.MP4", SearchOption.TopDirectoryOnly); this.Dispatcher.Invoke(new Action(() => { drive.Button.IsEnabled = false; if (files.Length == 0) { drive.ComState.ProgressMax(); drive.ComState.StateNothing(); } else { drive.ComState.StateCoping(); drive.ComState.SetMaxProgress(files.Length); drive.ComState.ProgressReSet(); } })); if (files.Length > 0) { DoCopy(files); } } catch (Exception ex) { //主要是U盘意外插拔错误 MessageBox.Show(ex.Message); return; } }