public async Task RefreshAsync(CancellationToken cancellationToken, int initialRealizedFileSystemTreeLevels = defaultInitializationDepth) { if (initialRealizedFileSystemTreeLevels < 1) { throw new ArgumentOutOfRangeException("Level count can't be smaller than 1"); } InternalDrives.Clear(); FileSystemMount.ChildFileSystemItems.Clear(); InternalFileSystemRoot.Clear(); await InitializeFileSystemTreeAsync(cancellationToken, initialRealizedFileSystemTreeLevels); }
private bool RealizeFromRoot(int levelCount, CancellationToken cancellationToken, bool isSortingEnabled) { bool hasRealizedItem = false; var nextLevelBreadthFirstQueue = new Queue <IDirectory>(InternalFileSystemRoot .OfType <IDrive>() .Where(rootItem => rootItem.IsReady)); int remainingLevels = levelCount; while (remainingLevels > 0 && nextLevelBreadthFirstQueue.Any()) { remainingLevels--; var currentLevelBreadthFirstQueue = new Queue <IDirectory>(nextLevelBreadthFirstQueue); nextLevelBreadthFirstQueue.Clear(); while (currentLevelBreadthFirstQueue.TryDequeue(out IDirectory parentDirectory)) { parentDirectory.ChildFileSystemItems.Clear(); foreach (FileSystemInfo childInfo in parentDirectory.Info.EnumerateFileSystemInfos("*", FileSystemEnumerationOptions)) { IFileSystemItemModel childItem; if (childInfo is DirectoryInfo directory) { childItem = DirectoryFactory.Invoke(directory, parentDirectory); nextLevelBreadthFirstQueue.Enqueue((IDirectory)childItem); } else { childItem = FileFactory.Invoke(childInfo as FileInfo, parentDirectory); } IndexFileSystemItem(childItem); parentDirectory.ChildFileSystemItems.Add(childItem); hasRealizedItem = true; } if (isSortingEnabled) { parentDirectory.Sort(); } } cancellationToken.ThrowIfCancellationRequested(); } return(hasRealizedItem); }
public async Task InitializeFileSystemTreeAsync(CancellationToken cancellationToken, int initialRealizedFileSystemTreeLevels = defaultInitializationDepth, bool isSortingEnabled = false) { if (initialRealizedFileSystemTreeLevels < 1) { throw new ArgumentOutOfRangeException("Level count can't be smaller than 1"); } await Task.Run(() => { foreach (DriveInfo drive in DriveInfo.GetDrives()) { InternalDrives.Add(drive); } var rootItems = CreateFileSystemRootItems(InternalDrives, cancellationToken); foreach (IFileSystemItemModel rootItem in rootItems) { FileSystemMount.ChildFileSystemItems.Add(rootItem); InternalFileSystemRoot.Add(rootItem); } RealizeFromRoot(initialRealizedFileSystemTreeLevels, cancellationToken, isSortingEnabled); }, cancellationToken); }