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"); } }