/// <summary> /// /// </summary> /// <param name="folderToSearchIn"></param> /// <returns></returns> /// <exception cref="FolderNotFoundException"></exception> /// <exception cref="InvalidPathException"></exception> /// <exception cref="ObjectDisposedException"></exception> private FileInfo SearchFileInFolderRecursively(FolderInfo folderToSearchIn) { try { RefreshCurrentFolder(folderToSearchIn); if (!_idsOfFilesAndFoldersSeenSoFar.Contains(folderToSearchIn.Id)) { foreach (FileInfo fileInfo in _filesOfCurrentFolder) { if (!_idsOfFilesAndFoldersSeenSoFar.Contains(fileInfo.Id)) { _idsOfFilesAndFoldersSeenSoFar.Add(fileInfo.Id); if (_wildcardForMatchingNamesOfFiles.CheckStringForMatch(fileInfo.Name)) { return(fileInfo); } } } foreach (FolderInfo folder in _subfoldersOfCurrentFolder) { if (!_idsOfFilesAndFoldersSeenSoFar.Contains(folder.Id)) { FileInfo fileFromFolder = SearchFileInFolderRecursively(folder); if (fileFromFolder != null) { return(fileFromFolder); } } } _idsOfFilesAndFoldersSeenSoFar.Add(folderToSearchIn.Id); // пошли к parent-y if (_folderToEnumerateFilesIn.Id != folderToSearchIn.Id) { try { FolderInfo parent = _filesAndFoldersProvider.GetParentOf(folderToSearchIn.FullPath); FileInfo fileFromFolder = this.SearchFileInFolderRecursively(parent); if (fileFromFolder != null) { return(fileFromFolder); } } catch (InvalidPathException) { return(null); } catch (CannotResolvePathException) { throw new InvalidPathException(); } } } } catch (ObjectDisposedException) { this.Dispose(); throw new ObjectDisposedException("Продолжение перебора файлов невозможно: файловая система уже была принудительно закрыта (путем вызова метода Dispose())."); } catch (FolderNotFoundException) { _enumeratedStructureHasChanged = true; throw CreateEnumerationNotPossibleException(); } catch (InvalidPathException) { _enumeratedStructureHasChanged = true; throw CreateEnumerationNotPossibleException(); } return(null); }