public static async Task DeleteAsync(string path, bool recursive) { var watcher = new DeleteFolderAsync(path); DeleteAndPreventAlreadyInUse.DeleteDirectory(path, recursive); await watcher.Sem.WaitAsync(); watcher.Dispose(); }
private static void DeleteDirectory(string path, bool recursive, bool asAlreadyRetry) { try { Directory.Delete(path, recursive); } catch (IOException e) { //If as already retry or if the exception is not an already in use exception (for example a directory not found exception) if (asAlreadyRetry || e.GetType() != typeof(IOException)) { throw; } GC.Collect(); GC.WaitForPendingFinalizers(); DeleteAndPreventAlreadyInUse.DeleteDirectory(path, recursive, true); } }