private static IEnumerable <IDirectoryInfo> GetDirectories(IDirectoryInfo root, int level, int maxDepth) { IEnumerable <IDirectoryInfo> subDirs = null; if (maxDepth >= 0 && level > maxDepth) { yield break; } try { subDirs = root.EnumerateDirectories(); } catch (Exception) { yield break; } foreach (IDirectoryInfo dirInfo in subDirs) { yield return(dirInfo); foreach (var recursiveDir in GetDirectories(dirInfo, level + 1, maxDepth)) { yield return(recursiveDir); } } }
/// <summary> /// Gets the current start index for indexed DB's /// </summary> /// <param name="path">Main path to DB directory.</param> /// <returns>Current - starting index of DB.</returns> private int GetStartingIndex(string path) { // gets path to non-index DB. string fullPath = _rocksDbFactory.GetFullDbPath(new RocksDbSettings(string.Empty, path)); IDirectoryInfo directory = _fileSystem.DirectoryInfo.FromDirectoryName(fullPath); if (directory.Exists) { if (directory.EnumerateFiles().Any()) { return(-2); // if there are files in the directory, then we have a main DB, marked -2. } // else we have sub-directories, which should be index based // we want to find lowest positive index and return it - 1. int minIndex = directory.EnumerateDirectories() .Select(d => int.TryParse(d.Name, out int index) ? index : -1) .Where(i => i >= 0) .OrderBy(i => i) .FirstOrDefault(); return(minIndex - 1); } return(-1); // if directory doesn't exist current index is -1. }
private static void RecursiveDelete(IDirectoryInfo directoryInfo) { if (!directoryInfo.Exists) { return; } directoryInfo.EnumerateDirectories().ForEach(dir => RecursiveDelete(dir)); directoryInfo.Delete(true); }
public void TraverseDirectory(string dn, string rw) { IDirectoryInfo dirInfo = this.fileSystem.DirectoryInfo.FromDirectoryName(dn); foreach (IDirectoryInfo di in dirInfo.EnumerateDirectories()) { Redactr r = new Redactr(this.fileSystem); r.TraverseDirectory(di.FullName, rw); } foreach (IFileInfo fi in dirInfo.EnumerateFiles()) { tasks.Add(Task.Factory.StartNew(() => { Redactor.Redact(new RedactData(this.fileSystem, fi.FullName, rw, '*')); })); } }
private IEnumerable <DisplayComponentModel> GetDisplayComponentFolders(string installerRoot) { IDirectoryInfo installerRootDirectory = _fileSystem.DirectoryInfo.FromDirectoryName(installerRoot); if (!installerRootDirectory.Exists) { throw new DirectoryNotFoundException("Installer folder does not exist or cannot access."); } foreach (IFileInfo cccInstallFile in installerRootDirectory.EnumerateFiles("ccc2_install.exe", SearchOption.AllDirectories)) { IDirectoryInfo displayDriverFolder = cccInstallFile.Directory.Parent; foreach (IDirectoryInfo componentDirectory in displayDriverFolder.EnumerateDirectories("*", SearchOption.TopDirectoryOnly)) { if (componentDirectory.GetFiles("*.inf", SearchOption.TopDirectoryOnly).Length == 1) { yield return(new DisplayComponentModel(installerRootDirectory, componentDirectory)); } } } }
/// <summary> /// implementation of post-order traversal of directory tree /// it is done this way to guarantee that by the time we finish any directory /// all of its subdirectories/files had been visited /// Note: /// if operaiton is cancelled - not all notifications will be emited, /// and namespace information will be partial. /// </summary> /// <param name="root">directory to scan</param> /// <param name="cancelationCallback">function to consult with for cancelation</param> /// <exception cref="System.IO.DirectoryNotFoundException">Cannot access directory: {root.FullName}</exception> private void EnumeratePostOrderNonRecursive(IDirectoryInfo root, Func <bool> cancelationCallback = null) { // handle the case than this is actually network share. if (!root.Exists()) { throw new System.IO.DirectoryNotFoundException(string.Format(StorageSyncResources.NamespaceEnumeratorErrorFormat, root.FullName)); } _namespaceInfo = new NamespaceInfo { Path = root.FullName, IsComplete = false }; Stack <IDirectoryInfo> stack1 = new Stack <IDirectoryInfo>(5000); Stack <IDirectoryInfo> stack2 = new Stack <IDirectoryInfo>(5000); Func <bool> shouldCancel = () => { if (System.Diagnostics.Debugger.IsAttached) { return(false); } return(cancelationCallback == null ? false : cancelationCallback.Invoke()); }; stack1.Push(root); _namespaceInfo.NumberOfDirectories++; while (stack1.Count > 0) { if (shouldCancel()) { return; } IDirectoryInfo currentDirectory = stack1.Pop(); stack2.Push(currentDirectory); // notify we have started processing directory // processing means accessing subdirectories and files NotifyBeginDir(currentDirectory); IList <IDirectoryInfo> subdirs = null; try { subdirs = new List <IDirectoryInfo>(currentDirectory.EnumerateDirectories()); } catch (UnauthorizedAccessException) { NotifyUnauthorizedDir(currentDirectory); continue; } NotifyNamespaceHints(subdirs.Count, 0); foreach (IDirectoryInfo subdir in subdirs) { stack1.Push(subdir); _namespaceInfo.NumberOfDirectories++; } } while (stack2.Count > 0) { if (shouldCancel()) { return; } IDirectoryInfo currentDirectory = stack2.Pop(); IList <IFileInfo> files = new List <IFileInfo>(currentDirectory.EnumerateFiles()); NotifyNamespaceHints(0, files.Count); foreach (IFileInfo file in files) { _namespaceInfo.NumberOfFiles++; _namespaceInfo.TotalFileSizeInBytes += file.Length; NotifyNextFile(file); } // notify we have finished processing directory NotifyEndDir(currentDirectory); } _namespaceInfo.IsComplete = true; }
public override void SearchForDicomDirectories(string rootDir) { Logger.Info("Starting directory scan of: " + rootDir); IsProcessing = true; TotalSent = 0; if (!FileSystem.Directory.Exists(rootDir)) { throw new DirectoryNotFoundException("Could not find the root directory at the start of the scan \"" + rootDir + "\""); } // Check if we were given an accession directory if (_accDirectoryRegex.IsMatch(rootDir)) { Logger.Debug("Given an accession directory, sending single message"); FoundNewDicomDirectory(rootDir.Remove(0, FileSystemRoot.Length)); } else { Times = new List <List <long> >(); for (var i = 0; i < 6; ++i) { Times.Add(new List <long>()); } var dirStack = new Stack <string>(); dirStack.Push(rootDir); while (dirStack.Count > 0 && !TokenSource.IsCancellationRequested) { string dir = dirStack.Pop(); Logger.Debug("Scanning " + dir); IDirectoryInfo dirInfo = FileSystem.DirectoryInfo.FromDirectoryName(dir); if (!dirInfo.Exists) { Logger.Warn("Can no longer find " + dir + ", continuing"); continue; } IEnumerable <IDirectoryInfo> subDirs = dirInfo.EnumerateDirectories(); if (_dayDirectoryRegex.IsMatch(dir)) { Logger.Debug("At the day level, assuming all subdirs are accession directories"); // At the day level, so each of the subdirectories will be accession directories foreach (IDirectoryInfo accessionDir in subDirs) { FoundNewDicomDirectory(accessionDir.FullName); } } else { Logger.Debug("Not at the day level, checking subdirectories"); subDirs.ToList().ForEach(x => dirStack.Push(x.FullName)); } } } IsProcessing = false; Logger.Info("Directory scan finished"); Logger.Info("Total messages sent: " + TotalSent); }