public static void CopyDirectory(this FileSystemManager fs, string sourcePath, string destPath, CreateFileOptions options = CreateFileOptions.None, IProgressReport logger = null) { using (DirectoryHandle sourceHandle = fs.OpenDirectory(sourcePath, OpenDirectoryMode.All)) { foreach (DirectoryEntry entry in fs.ReadDirectory(sourceHandle)) { string subSrcPath = PathTools.Normalize(PathTools.Combine(sourcePath, entry.Name)); string subDstPath = PathTools.Normalize(PathTools.Combine(destPath, entry.Name)); if (entry.Type == DirectoryEntryType.Directory) { fs.EnsureDirectoryExists(subDstPath); fs.CopyDirectory(subSrcPath, subDstPath, options, logger); } if (entry.Type == DirectoryEntryType.File) { logger?.LogMessage(subSrcPath); fs.CreateOrOverwriteFile(subDstPath, entry.Size, options); fs.CopyFile(subSrcPath, subDstPath, logger); } } } }
public static IEnumerable <DirectoryEntry> EnumerateEntries(this FileSystemManager fs, string path, string searchPattern, SearchOptions searchOptions) { bool ignoreCase = searchOptions.HasFlag(SearchOptions.CaseInsensitive); bool recurse = searchOptions.HasFlag(SearchOptions.RecurseSubdirectories); using (DirectoryHandle sourceHandle = fs.OpenDirectory(path, OpenDirectoryMode.All)) { foreach (DirectoryEntry entry in fs.ReadDirectory(sourceHandle)) { if (PathTools.MatchesPattern(searchPattern, entry.Name, ignoreCase)) { yield return(entry); } if (entry.Type != DirectoryEntryType.Directory || !recurse) { continue; } string subPath = PathTools.Normalize(PathTools.Combine(path, entry.Name)); IEnumerable <DirectoryEntry> subEntries = fs.EnumerateEntries(subPath, searchPattern, searchOptions); foreach (DirectoryEntry subEntry in subEntries) { subEntry.FullPath = PathTools.Combine(path, subEntry.Name); yield return(subEntry); } } } }