fix: 无法删除部分文件夹(Meloong-Git/PCL#8878,@Peacock-Que)
This commit is contained in:
1 parent
c16ef849e6
commit
cf42cf19e3
2 files changed
+13
-1
No files matched your search
@@ -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[] {
|
||||
|
||||
@@ -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
|
||||
|
||||
}
|
||||
Reference in new issue
Block a user