public void DeleteDirectory(string path, DeletionOptions options) { options = options ?? DeletionOptions.TryThreeTimes; if (string.IsNullOrWhiteSpace(path)) { return; } for (int index = 0; index < options.RetryAttempts; ++index) { try { if (Directory.Exists(path)) { DirectoryInfo directoryInfo = new DirectoryInfo(path); directoryInfo.Attributes = directoryInfo.Attributes & ~FileAttributes.ReadOnly; directoryInfo.Delete(true); } } catch { Thread.Sleep(options.SleepBetweenAttemptsMilliseconds); if (index == options.RetryAttempts - 1) { if (!options.ThrowOnFailure) { break; } throw; } } } }
public void DeleteFile(string path, DeletionOptions options) { options = options ?? DeletionOptions.TryThreeTimes; if (string.IsNullOrWhiteSpace(path)) { return; } bool flag = false; for (int index = 0; index < options.RetryAttempts; ++index) { try { if (File.Exists(path)) { if (flag) { File.SetAttributes(path, FileAttributes.Normal); } File.Delete(path); } } catch { Thread.Sleep(options.SleepBetweenAttemptsMilliseconds); flag = true; if (index == options.RetryAttempts - 1) { if (!options.ThrowOnFailure) { break; } throw; } } } }
private void PurgeDirectory(string targetDirectory, Predicate <IFileInfo> include, DeletionOptions options, CancellationToken cancel, bool includeTarget = false) { if (!DirectoryExists(targetDirectory)) { return; } foreach (string str in EnumerateFiles(targetDirectory)) { cancel.ThrowIfCancellationRequested(); if (include != null) { FileInfoAdapter fileInfoAdapter = new FileInfoAdapter(new FileInfo(str)); if (!include(fileInfoAdapter)) { continue; } } DeleteFile(str, options); } foreach (string str in EnumerateDirectories(targetDirectory)) { cancel.ThrowIfCancellationRequested(); if ((new DirectoryInfo(str).Attributes & FileAttributes.ReparsePoint) == FileAttributes.ReparsePoint) { Directory.Delete(str); } else { PurgeDirectory(str, include, options, cancel, true); } } if (!includeTarget || !DirectoryIsEmpty(targetDirectory)) { return; } DeleteDirectory(targetDirectory, options); }
public void PurgeDirectory(string targetDirectory, Predicate <IFileInfo> include, DeletionOptions options) { PurgeDirectory(targetDirectory, include, options, CancellationToken.None); }
public void PurgeDirectory(string targetDirectory, DeletionOptions options, CancellationToken cancel) { PurgeDirectory(targetDirectory, fi => true, options, cancel); }
public void PurgeDirectory(string targetDirectory, DeletionOptions options) { PurgeDirectory(targetDirectory, fi => true, options); }