void PurgeDirectory(string targetDirectory, Predicate <IFileInfo> include, DeletionOptions options, CancellationToken cancel, bool includeTarget = false) { if (!DirectoryExists(targetDirectory)) { return; } foreach (var file in EnumerateFiles(targetDirectory)) { cancel.ThrowIfCancellationRequested(); if (include != null) { var info = new FileInfoAdapter(new FileInfo(file)); if (!include(info)) { continue; } } DeleteFile(file, options); } foreach (var directory in EnumerateDirectories(targetDirectory)) { cancel.ThrowIfCancellationRequested(); var info = new DirectoryInfo(directory); if ((info.Attributes & FileAttributes.ReparsePoint) == FileAttributes.ReparsePoint) { Directory.Delete(directory); } else { PurgeDirectory(directory, include, options, cancel, includeTarget: true); } } if (includeTarget && DirectoryIsEmpty(targetDirectory)) { DeleteDirectory(targetDirectory, options); } }
void PurgeDirectory(string targetDirectory, Predicate<IFileInfo> include, DeletionOptions options, CancellationToken cancel, bool includeTarget = false) { if (!DirectoryExists(targetDirectory)) { return; } foreach (var file in EnumerateFiles(targetDirectory)) { cancel.ThrowIfCancellationRequested(); if (include != null) { var info = new FileInfoAdapter(new FileInfo(file)); if (!include(info)) { continue; } } DeleteFile(file, options); } foreach (var directory in EnumerateDirectories(targetDirectory)) { cancel.ThrowIfCancellationRequested(); var info = new DirectoryInfo(directory); if ((info.Attributes & FileAttributes.ReparsePoint) == FileAttributes.ReparsePoint) { Directory.Delete(directory); } else { PurgeDirectory(directory, include, options, cancel, includeTarget: true); } } if (includeTarget && DirectoryIsEmpty(targetDirectory)) DeleteDirectory(targetDirectory, options); }