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 DeleteFile(string path, bool asAlreadyRetry) { try { File.Delete(path); } catch (IOException e) { //If as already retry or if the exception is not an already in use exception (for example a file not found exception) if (asAlreadyRetry || e.GetType() != typeof(IOException)) { throw; } GC.Collect(); GC.WaitForPendingFinalizers(); DeleteAndPreventAlreadyInUse.DeleteFile(path, true); } }