From a3e545281eaac845a4903c60eaef7e89a0401434 Mon Sep 17 00:00:00 2001 From: Timothy Baldridge Date: Sun, 19 Dec 2021 14:52:06 -0700 Subject: [PATCH] More complex directory deletion --- Wabbajack.Paths.IO/AbsolutePathExtensions.cs | 19 ++++++++++++++++++- Wabbajack.Server/DataModels/AuthorFiles.cs | 2 -- 2 files changed, 18 insertions(+), 3 deletions(-) diff --git a/Wabbajack.Paths.IO/AbsolutePathExtensions.cs b/Wabbajack.Paths.IO/AbsolutePathExtensions.cs index 5ef71f1d..1320d8d3 100644 --- a/Wabbajack.Paths.IO/AbsolutePathExtensions.cs +++ b/Wabbajack.Paths.IO/AbsolutePathExtensions.cs @@ -187,7 +187,24 @@ public static class AbsolutePathExtensions { if (!path.DirectoryExists()) return; if (dontDeleteIfNotEmpty && (path.EnumerateFiles().Any() || path.EnumerateDirectories().Any())) return; - Directory.Delete(ToNativePath(path), true); + + foreach (string directory in Directory.GetDirectories(path.ToString())) + { + DeleteDirectory(directory.ToAbsolutePath(), dontDeleteIfNotEmpty); + } + + try + { + Directory.Delete(path.ToString(), true); + } + catch (IOException) + { + Directory.Delete(path.ToString(), true); + } + catch (UnauthorizedAccessException) + { + Directory.Delete(path.ToString(), true); + } } public static bool DirectoryExists(this AbsolutePath path) diff --git a/Wabbajack.Server/DataModels/AuthorFiles.cs b/Wabbajack.Server/DataModels/AuthorFiles.cs index b28bdef9..51ce6545 100644 --- a/Wabbajack.Server/DataModels/AuthorFiles.cs +++ b/Wabbajack.Server/DataModels/AuthorFiles.cs @@ -91,8 +91,6 @@ public class AuthorFiles public async Task DeleteFile(FileDefinition definition) { var folder = AuthorFilesLocation.Combine(definition.MungedName); - foreach (var file in folder.EnumerateFiles()) - file.Delete(); folder.DeleteDirectory(); }