private IEnumerable <FileSystemInfo> ScanDir(DirectoryInfo dir, int level) { if (_breakNexStep) { yield break; } DirectoryFinded?.Invoke(dir, _logService); yield return(dir); FileInfo[] files = _workTree.GetFiles(dir); foreach (FileInfo f in files) { if (_breakNexStep) { yield break; } FileFinded?.Invoke(f, _logService); yield return(f); } DirectoryInfo[] subDirs = _workTree.GetDirectories(dir); foreach (DirectoryInfo d in subDirs) { foreach (var s in ScanDir(d, ++level)) { yield return(s); } } }
public void FileSystemVisitorWithoutFilterTest() { _fileSystemVisitor = new FileSystemVisitor(@"C:\Test", _workTree, _logService); _workTree.GetFiles(_fileSystemVisitor.ValRootDirectory).Returns(_filesInRoot); _workTree.GetFiles(_subDir1).Returns(_fileInSubDir1); _workTree.GetFiles(_subDir2).Returns(_fileInSubDir2); _workTree.GetDirectories(_fileSystemVisitor.ValRootDirectory).Returns(_subDirs); _workTree.GetDirectories(_subDir1).Returns(new DirectoryInfo[] { }); _workTree.GetDirectories(_subDir2).Returns(new DirectoryInfo[] { }); FileSystemInfo[] allMustFindObjects = new FileSystemInfo[] { _fileSystemVisitor.ValRootDirectory, _fileInRoot1, _fileInRoot2, _fileInRoot3, _subDir1, _fileInSubDir1_1, _subDir2, _fileInSubDir2_1, _fileInSubDir2_2 }; int i = 0; FileSystemInfo[] FindObjects = new FileSystemInfo[9]; //using (StreamWriter file = new StreamWriter(@"C:\Users\Public\resultTest1.txt")) //{ foreach (var s in _fileSystemVisitor.ScanDir(_fileSystemVisitor.ValRootDirectory)) { //file.WriteLine(s.FullName); Assert.AreEqual(allMustFindObjects[i], s); FindObjects[i] = s; i++; } //} CollectionAssert.AreEqual(allMustFindObjects, FindObjects); Assert.AreEqual(i, 9); }