public void DeleteFile(string path, FailureOptions options)
        {
            if (string.IsNullOrWhiteSpace(path))
                return;

            var retry = GetRetryTracker();

            while (retry.Try())
            {
                try
                {
                    if (File.Exists(path))
                    {
                        if (retry.IsNotFirstAttempt)
                        {
                            File.SetAttributes(path, FileAttributes.Normal);
                        }
                        File.Delete(path);
                    }
                    break;
                }
                catch (Exception ex)
                {
                    if (retry.CanRetry())
                    {
                        if (retry.ShouldLogWarning())
                        {
                            Log.VerboseFormat("Retry #{0} on delete file '{1}'. Exception: {2}", retry.CurrentTry, path, ex.Message);
                        }
                        Thread.Sleep(retry.Sleep());
                    }
                    else
                    {
                        if (options == FailureOptions.ThrowOnFailure)
                        {
                            throw;
                        }
                    }
                }
            }
        }
        void PurgeDirectory(string targetDirectory, Predicate<IFileInfo> include, FailureOptions 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);
        }
        static void EnsureDirectoryDeleted(string path, FailureOptions failureOptions)
        {
            var retry = GetRetryTracker();

            while (retry.Try())
            {
                if (!Directory.Exists(path))
                    return;

                if (retry.CanRetry() && retry.ShouldLogWarning())
                    Log.VerboseFormat("Waiting for directory '{0}' to be deleted", path);
            }

            var message = $"Directory '{path}' still exists, despite requested deletion";

            if (failureOptions == FailureOptions.ThrowOnFailure)
                throw new Exception(message);

            Log.Verbose(message);
        }
 public void PurgeDirectory(string targetDirectory, Predicate<IFileInfo> include, FailureOptions options)
 {
     PurgeDirectory(targetDirectory, include, options, CancellationToken.None);
 }
 public void PurgeDirectory(string targetDirectory, FailureOptions options, CancellationToken cancel)
 {
     PurgeDirectory(targetDirectory, fi => true, options, cancel);
 }
 public void PurgeDirectory(string targetDirectory, FailureOptions options)
 {
     PurgeDirectory(targetDirectory, fi => true, options);
 }
        public void DeleteDirectory(string path, FailureOptions options)
        {
            if (string.IsNullOrWhiteSpace(path))
                return;

            var retry = GetRetryTracker();
            while (retry.Try())
            {
                try
                {
                    if (Directory.Exists(path))
                    {
                        var dir = new DirectoryInfo(path);
                        dir.Attributes = dir.Attributes & ~FileAttributes.ReadOnly;
                        dir.Delete(true);
                        EnsureDirectoryDeleted(path, options);
                    }
                    break;
                }
                catch (Exception ex)
                {
                    if (retry.CanRetry())
                    {
                        if (retry.ShouldLogWarning())
                        {
                            Log.VerboseFormat("Retry #{0} on delete directory '{1}'. Exception: {2}", retry.CurrentTry, path, ex.Message);
                        }
                        Thread.Sleep(retry.Sleep());
                    }
                    else
                    {
                        if (options == FailureOptions.ThrowOnFailure)
                        {
                            throw;
                        }
                    }
                }
            }
        }