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(); }
private static async void PortalDeviceWatcher_Added(DeviceWatcher sender, DeviceInformation args) { try { StorageFolder DeviceFolder = StorageDevice.FromId(args.Id); if (DriveList.All((Device) => (string.IsNullOrEmpty(Device.Path) || string.IsNullOrEmpty(DeviceFolder.Path)) ? !Device.Name.Equals(DeviceFolder.Name, StringComparison.OrdinalIgnoreCase) : !Device.Path.Equals(DeviceFolder.Path, StringComparison.OrdinalIgnoreCase))) { await CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, async() => { DriveList.Add(await DriveDataBase.CreateAsync(DeviceFolder, DriveType.Removable)); }); } } catch (Exception ex) { LogTracer.Log(ex, $"Error happened when add device to HardDeviceList"); } }
public static async Task LoadDriveAsync(bool IsRefresh = false) { if (Interlocked.Exchange(ref LoadDriveLockResource, 1) == 0) { try { if (!IsDriveLoaded || IsRefresh) { IsDriveLoaded = true; foreach (DriveInfo Drive in DriveInfo.GetDrives().Where((Drives) => Drives.DriveType == DriveType.Fixed || Drives.DriveType == DriveType.Removable || Drives.DriveType == DriveType.Network) .Where((NewItem) => DriveList.All((Item) => !Item.Path.Equals(NewItem.RootDirectory.FullName, StringComparison.OrdinalIgnoreCase)))) { try { StorageFolder Folder = await StorageFolder.GetFolderFromPathAsync(Drive.RootDirectory.FullName); if (DriveList.All((Item) => (string.IsNullOrEmpty(Item.Path) || string.IsNullOrEmpty(Folder.Path)) ? !Item.Name.Equals(Folder.Name, StringComparison.OrdinalIgnoreCase) : !Item.Path.Equals(Folder.Path, StringComparison.OrdinalIgnoreCase))) { DriveList.Add(await DriveDataBase.CreateAsync(Folder, Drive.DriveType)); } } catch (Exception ex) { LogTracer.Log(ex, $"Hide the device \"{Drive.RootDirectory.FullName}\" for error"); } } foreach (DeviceInformation Drive in await DeviceInformation.FindAllAsync(StorageDevice.GetDeviceSelector())) { try { StorageFolder Folder = StorageDevice.FromId(Drive.Id); if (DriveList.All((Item) => (string.IsNullOrEmpty(Item.Path) || string.IsNullOrEmpty(Folder.Path)) ? !Item.Name.Equals(Folder.Name, StringComparison.OrdinalIgnoreCase) : !Item.Path.Equals(Folder.Path, StringComparison.OrdinalIgnoreCase))) { DriveList.Add(await DriveDataBase.CreateAsync(Folder, DriveType.Removable)); } } catch (Exception ex) { LogTracer.Log(ex, $"Hide the device for error"); } } foreach (StorageFolder Folder in await GetWslDriveAsync()) { try { if (DriveList.All((Item) => (string.IsNullOrEmpty(Item.Path) || string.IsNullOrEmpty(Folder.Path)) ? !Item.Name.Equals(Folder.Name, StringComparison.OrdinalIgnoreCase) : !Item.Path.Equals(Folder.Path, StringComparison.OrdinalIgnoreCase))) { DriveList.Add(await DriveDataBase.CreateAsync(Folder, DriveType.Network)); } } catch (Exception ex) { LogTracer.Log(ex, $"Hide the device \"{Folder.Name}\" for error"); } } switch (PortalDeviceWatcher.Status) { case DeviceWatcherStatus.Created: case DeviceWatcherStatus.Aborted: case DeviceWatcherStatus.Stopped: { PortalDeviceWatcher.Start(); break; } } if (!NetworkDriveCheckTimer.IsEnabled) { NetworkDriveCheckTimer.Start(); } } } catch (Exception ex) { LogTracer.Log(ex, $"An exception was threw in {nameof(LoadDriveAsync)}"); } finally { _ = Interlocked.Exchange(ref LoadDriveLockResource, 0); } } }