private async static void NetworkDriveCheckTimer_Tick(object sender, object e) { NetworkDriveCheckTimer.Stop(); DriveInfo[] NewNetworkDrive = DriveInfo.GetDrives().Where((Drives) => Drives.DriveType == DriveType.Network).ToArray(); DriveDataBase[] ExistNetworkDrive = DriveList.OfType <NormalDriveData>().Where((ExistDrive) => ExistDrive.DriveType == DriveType.Network).ToArray(); IEnumerable <DriveInfo> AddList = NewNetworkDrive.Where((NewDrive) => ExistNetworkDrive.All((ExistDrive) => !ExistDrive.Path.Equals(NewDrive.RootDirectory.FullName, StringComparison.OrdinalIgnoreCase))); IEnumerable <DriveDataBase> RemoveList = ExistNetworkDrive.Where((ExistDrive) => NewNetworkDrive.All((NewDrive) => !ExistDrive.Path.Equals(NewDrive.RootDirectory.FullName, StringComparison.OrdinalIgnoreCase))); foreach (DriveDataBase ExistDrive in RemoveList) { DriveList.Remove(ExistDrive); } foreach (DriveInfo Drive in AddList) { try { StorageFolder Device = await StorageFolder.GetFolderFromPathAsync(Drive.RootDirectory.FullName); DriveList.Add(await DriveDataBase.CreateAsync(Device, Drive.DriveType)); } catch (Exception ex) { LogTracer.Log(ex, $"Hide the device \"{Drive.RootDirectory.FullName}\" for error"); } } NetworkDriveCheckTimer.Start(); }
/// <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; } }
private async static void NetworkDriveCheckTimer_Tick(object sender, object e) { NetworkDriveCheckTimer.Stop(); DriveInfo[] NewNetworkDrive = DriveInfo.GetDrives().Where((Drives) => Drives.DriveType == DriveType.Network).ToArray(); DriveRelatedData[] ExistNetworkDrive = DriveList.Where((ExistDrive) => ExistDrive.DriveType == DriveType.Network).ToArray(); IEnumerable <DriveInfo> AddList = NewNetworkDrive.Where((NewDrive) => ExistNetworkDrive.All((ExistDrive) => ExistDrive.Folder.Path != NewDrive.RootDirectory.FullName)); IEnumerable <DriveRelatedData> RemoveList = ExistNetworkDrive.Where((ExistDrive) => NewNetworkDrive.All((NewDrive) => ExistDrive.Folder.Path != NewDrive.RootDirectory.FullName)); foreach (DriveRelatedData ExistDrive in RemoveList) { DriveList.Remove(ExistDrive); } foreach (DriveInfo Drive in AddList) { try { StorageFolder Device = await StorageFolder.GetFolderFromPathAsync(Drive.RootDirectory.FullName); DriveList.Add(await DriveRelatedData.CreateAsync(Device, Drive.DriveType)); } catch (Exception ex) { LogTracer.Log(ex, $"Hide the device \"{Drive.RootDirectory.FullName}\" for error"); } } NetworkDriveCheckTimer.Start(); }
private static async void PortalDeviceWatcher_Removed(DeviceWatcher sender, DeviceInformationUpdate args) { try { List <string> AllBaseDevice = DriveInfo.GetDrives().Where((Drives) => Drives.DriveType == DriveType.Fixed || Drives.DriveType == DriveType.Network) .Select((Info) => Info.RootDirectory.FullName).ToList(); List <StorageFolder> PortableDevice = new List <StorageFolder>(); foreach (DeviceInformation Device in await DeviceInformation.FindAllAsync(StorageDevice.GetDeviceSelector())) { try { PortableDevice.Add(StorageDevice.FromId(Device.Id)); } catch (Exception ex) { LogTracer.Log(ex, $"Error happened when get storagefolder from {Device.Name}"); } } foreach (string PortDevice in AllBaseDevice.Where((Path) => PortableDevice.Any((Item) => Item.Path.Equals(Path, StringComparison.OrdinalIgnoreCase)))) { AllBaseDevice.Remove(PortDevice); } List <DriveDataBase> OneStepDeviceList = DriveList.Where((Item) => !AllBaseDevice.Contains(Item.Path)).ToList(); List <DriveDataBase> TwoStepDeviceList = OneStepDeviceList.Where((RemoveItem) => PortableDevice.All((Item) => !Item.Name.Equals(RemoveItem.Name, StringComparison.OrdinalIgnoreCase))).ToList(); await CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () => { foreach (DriveDataBase Device in TwoStepDeviceList) { DriveList.Remove(Device); } }); } catch (Exception ex) { LogTracer.Log(ex, $"Error happened when remove device from HardDeviceList"); } }