fix: 驱动器根路径兼容

This commit is contained in:
龙腾猫跃 committed 2026-05-12 15:12:32 +08:00
1 parent 347a51f9d4
commit d4af33f5be
5 files changed
+51 -45

No files matched your search

+13 -14
View File
@@ -8,21 +8,20 @@ public static class DirectoryUtils {
public static void Create(string path) {
if (DirectoryUtils.Exists(path)) return;
Logger.Trace($"新建文件夹:{path}");
path = PathUtils.WithLongPath(path);
Directory.CreateDirectory(path);
Directory.CreateDirectory(PathUtils.ForApi(path));
}
/// <summary>
/// 判断文件夹是否存在。
/// </summary>
public static bool Exists(string path)
=> Directory.Exists(PathUtils.WithLongPath(path));
=> Directory.Exists(PathUtils.ForApi(path));
/// <summary>
/// 获取 <see cref="DirectoryInfo"/> 对象。
/// </summary>
public static DirectoryInfo GetInfo(string path)
=> new(PathUtils.WithLongPath(path));
=> new(PathUtils.ForApi(path));
/// <summary>
/// 返回指定路径下的所有文件,不以 \\?\ 开头。
@@ -30,7 +29,7 @@ public static class DirectoryUtils {
/// </summary>
public static IEnumerable<string> GetFiles(string path, bool topDirectoryOnly = false, string searchPattern = "*") {
if (!DirectoryUtils.Exists(path)) return [];
return Directory.EnumerateFiles(PathUtils.WithLongPath(path), searchPattern, topDirectoryOnly ? SearchOption.TopDirectoryOnly : SearchOption.AllDirectories).
return Directory.EnumerateFiles(PathUtils.ForApi(path), searchPattern, topDirectoryOnly ? SearchOption.TopDirectoryOnly : SearchOption.AllDirectories).
Select(PathUtils.WithoutLongPath);
}
@@ -40,7 +39,7 @@ public static class DirectoryUtils {
/// </summary>
public static IEnumerable<string> GetDirectories(string path, bool topDirectoryOnly = false, string searchPattern = "*") {
if (!DirectoryUtils.Exists(path)) return [];
return Directory.EnumerateDirectories(PathUtils.WithLongPath(path), searchPattern, topDirectoryOnly ? SearchOption.TopDirectoryOnly : SearchOption.AllDirectories).
return Directory.EnumerateDirectories(PathUtils.ForApi(path), searchPattern, topDirectoryOnly ? SearchOption.TopDirectoryOnly : SearchOption.AllDirectories).
Select(PathUtils.WithoutLongPath);
}
@@ -50,7 +49,7 @@ public static class DirectoryUtils {
/// </summary>
public static bool IsEmpty(string path) {
if (!DirectoryUtils.Exists(path)) return true;
return !Directory.EnumerateFileSystemEntries(PathUtils.WithLongPath(path)).Any();
return !Directory.EnumerateFileSystemEntries(PathUtils.ForApi(path)).Any();
}
/// <summary>
@@ -63,8 +62,8 @@ public static class DirectoryUtils {
progressHandler?.Invoke(1);
return;
}
sourceFolder = PathUtils.Normalize(sourceFolder, true);
destFolder = PathUtils.Normalize(destFolder, true);
sourceFolder = PathUtils.ForCompare(sourceFolder);
destFolder = PathUtils.ForCompare(destFolder);
int doneCount = 0;
Lazy<int> totalCount = new(() => DirectoryUtils.GetFiles(sourceFolder).Count());
Logger.Trace($"复制文件夹:{sourceFolder} → {destFolder}");
@@ -82,7 +81,7 @@ public static class DirectoryUtils {
if (!DirectoryUtils.Exists(sourceFolder)) return;
DirectoryUtils.Delete(destFolder); // Move 要求不存在对应文件夹
Logger.Trace($"剪切文件夹:{sourceFolder} → {destFolder}");
Directory.Move(PathUtils.WithLongPath(sourceFolder), PathUtils.WithLongPath(destFolder));
Directory.Move(PathUtils.ForApi(sourceFolder), PathUtils.ForApi(destFolder));
}
/// <summary>
@@ -92,8 +91,8 @@ public static class DirectoryUtils {
// 安全检查
if (!DirectoryUtils.Exists(folder)) return;
Logger.Trace($"{(toRecycleBin ? "将文件夹删除到回收站" : "删除文件夹")}:{folder}");
folder = PathUtils.Normalize(folder, true);
if (folder == Path.GetPathRoot(folder))
folder = PathUtils.ForCompare(folder);
if (folder == PathUtils.ForCompare(Path.GetPathRoot(folder)))
throw new UnauthorizedAccessException($"不应删除磁盘根目录:{folder}");
if (criticalFolders.Value.Any(f => f.StartsWithF(folder, ignoreCase: true)))
throw new UnauthorizedAccessException($"不应删除的文件夹:{folder}");
@@ -104,7 +103,7 @@ public static class DirectoryUtils {
DeleteInternal(folder); // 永久删除
static void DeleteInternal(string folder) {
try {
folder = PathUtils.WithLongPath(folder);
folder = PathUtils.ForApi(folder);
foreach (string filePath in DirectoryUtils.GetFiles(folder, true)) FileUtils.Delete(filePath); // 删除文件
foreach (string str in DirectoryUtils.GetDirectories(folder, true)) DeleteInternal(str); // 递归删除子文件夹
ResilientUtils.RetryOn<IOException>(() => Directory.Delete(folder, true)); // 最终删除文件夹
@@ -125,6 +124,6 @@ public static class DirectoryUtils {
Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory),
Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments),
Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile), "Downloads") // 下载文件夹没有 SpecialFolder 枚举
}.Select(f => PathUtils.Normalize(f, true))));
}.Select(f => PathUtils.ForCompare(f))));
}