summaryrefslogtreecommitdiff
path: root/src/SMAPI.Toolkit/Utilities/FileUtilities.cs
diff options
context:
space:
mode:
Diffstat (limited to 'src/SMAPI.Toolkit/Utilities/FileUtilities.cs')
-rw-r--r--src/SMAPI.Toolkit/Utilities/FileUtilities.cs46
1 files changed, 46 insertions, 0 deletions
diff --git a/src/SMAPI.Toolkit/Utilities/FileUtilities.cs b/src/SMAPI.Toolkit/Utilities/FileUtilities.cs
new file mode 100644
index 00000000..7856fdb1
--- /dev/null
+++ b/src/SMAPI.Toolkit/Utilities/FileUtilities.cs
@@ -0,0 +1,46 @@
+using System.IO;
+using System.Threading;
+
+namespace StardewModdingAPI.Toolkit.Utilities
+{
+ /// <summary>Provides utilities for dealing with files.</summary>
+ public static class FileUtilities
+ {
+ /*********
+ ** Public methods
+ *********/
+ /// <summary>Delete a file or folder regardless of file permissions, and block until deletion completes.</summary>
+ /// <param name="entry">The file or folder to reset.</param>
+ public static void ForceDelete(FileSystemInfo entry)
+ {
+ // ignore if already deleted
+ entry.Refresh();
+ if (!entry.Exists)
+ return;
+
+ // delete children
+ if (entry is DirectoryInfo folder)
+ {
+ foreach (FileSystemInfo child in folder.GetFileSystemInfos())
+ FileUtilities.ForceDelete(child);
+ }
+
+ // reset permissions & delete
+ entry.Attributes = FileAttributes.Normal;
+ entry.Delete();
+
+ // wait for deletion to finish
+ for (int i = 0; i < 10; i++)
+ {
+ entry.Refresh();
+ if (entry.Exists)
+ Thread.Sleep(500);
+ }
+
+ // throw exception if deletion didn't happen before timeout
+ entry.Refresh();
+ if (entry.Exists)
+ throw new IOException($"Timed out trying to delete {entry.FullName}");
+ }
+ }
+}