Bladeren bron

fix: 无法删除部分文件夹(Meloong-Git/PCL#8878,@Peacock-Que)

龙腾猫跃 3 maanden geleden
bovenliggende
commit
cf42cf19e3
2 gewijzigde bestanden met toevoegingen van 13 en 1 verwijderingen
  1. 1 1
      Shared/Utils/DirectoryUtils.cs
  2. 12 0
      Tests/Utils/DirectoryUtilsTest.cs

+ 1 - 1
Shared/Utils/DirectoryUtils.cs

@@ -169,7 +169,7 @@ public static class DirectoryUtils {
         folder = PathUtils.ForCompare(folder);
         if (folder == PathUtils.ForCompare(Path.GetPathRoot(folder)))
             throw new UnauthorizedAccessException($"不应操作磁盘根目录:{folder}");
-        if (criticalFolders.Value.Any(f => PathUtils.IsParentOf(f, folder)))
+        if (criticalFolders.Value.Any(f => PathUtils.IsParentOf(folder, f)))
             throw new UnauthorizedAccessException($"不应操作文件夹:{folder}");
     }
     private static readonly Lazy<HashSet<string>> criticalFolders = new(() => new(new[] {

+ 12 - 0
Tests/Utils/DirectoryUtilsTest.cs

@@ -75,6 +75,18 @@ public class DirectoryUtilsTest : TestWithFolder {
     public void 剪切_安全检查() 
         => Assert.Throws<UnauthorizedAccessException>(() => DirectoryUtils.Move(Paths.Base, Path.Combine(tempFolder, "SafetyCheckDest")));
 
+    [Test]
+    public async Task 删除_安全检查_允许程序目录下的子文件夹() {
+        var folder = Path.Combine(Paths.Base, $"MeloongTest_{Guid.NewGuid():N}");
+        try {
+            DirectoryUtils.Create(folder);
+            DirectoryUtils.Delete(folder);
+            await Assert.That(DirectoryUtils.Exists(folder)).IsFalse();
+        } finally {
+            if (Directory.Exists(PathUtils.ForApi(folder))) Directory.Delete(PathUtils.ForApi(folder), true);
+        }
+    }
+
     #endregion
 
 }