This commit is contained in:
LTCat committed 2026-10-09 00:09:50 +08:00
1 parent 52290ece97
commit 2a954ba52f
3 files changed
+15 -12

No files matched your search

+3 -3
View File
@@ -71,7 +71,7 @@ public static class DirectoryUtils {
if (string.CompareOrdinal(sourceFolder, destFolder) == 0) {
// 复制自身到自身,则不执行操作
Logger.Trace($"复制文件夹到自身,不执行操作:{sourceFolder} → {destFolder}");
} else if (string.Compare(sourceFolder, destFolder, StringComparison.OrdinalIgnoreCase) == 0) {
} else if (PathUtils.IsSame(sourceFolder, destFolder)) {
// 路径仅大小写不同,等效于重命名
Logger.Trace($"复制文件夹到自身,但大小写不同,等效于重命名文件夹:{sourceFolder} → {destFolder}");
DirectoryUtils.Move(sourceFolder, destFolder);
@@ -93,7 +93,7 @@ public static class DirectoryUtils {
if (string.CompareOrdinal(sourceFolder, destFolder) == 0) {
// 剪切自身到自身,则不执行操作
Logger.Trace($"剪切文件夹到自身,不执行操作:{sourceFolder} → {destFolder}");
} else if (string.Compare(sourceFolder, destFolder, StringComparison.OrdinalIgnoreCase) == 0) {
} else if (PathUtils.IsSame(sourceFolder, destFolder)) {
// 路径仅大小写不同
Logger.Trace($"剪切文件夹到自身,但大小写不同:{sourceFolder} → {destFolder}");
var temp = Path.Combine(PathUtils.RemoveLastPart(sourceFolder), Path.GetRandomFileName());
@@ -167,7 +167,7 @@ public static class DirectoryUtils {
/// <exception cref="UnauthorizedAccessException" />
private static void SafetyCheck(string folder) {
folder = PathUtils.ForCompare(folder);
if (folder == PathUtils.ForCompare(Path.GetPathRoot(folder)))
if (PathUtils.IsSame(folder, Path.GetPathRoot(folder)))
throw new UnauthorizedAccessException($"不应操作磁盘根目录:{folder}");
if (criticalFolders.Value.Any(f => PathUtils.IsParentOf(folder, f)))
throw new UnauthorizedAccessException($"不应操作文件夹:{folder}");
+2 -2
View File
@@ -188,7 +188,7 @@ public static class FileUtils {
if (string.CompareOrdinal(sourceFilePath, destFilePath) == 0) {
// 复制自身到自身,则不执行操作
Logger.Trace($"复制文件到自身,不执行操作:{sourceFilePath} → {destFilePath}");
} else if (string.Compare(sourceFilePath, destFilePath, StringComparison.OrdinalIgnoreCase) == 0) {
} else if (PathUtils.IsSame(sourceFilePath, destFilePath)) {
// 路径仅大小写不同,等效于重命名
Logger.Trace($"复制文件到自身,但大小写不同,等效于重命名文件:{sourceFilePath} → {destFilePath}");
FileUtils.Move(sourceFilePath, destFilePath);
@@ -213,7 +213,7 @@ public static class FileUtils {
if (string.CompareOrdinal(sourceFilePath, destFilePath) == 0) {
// 剪切自身到自身,则不执行操作
Logger.Trace($"剪切文件到自身,不执行操作:{sourceFilePath} → {destFilePath}");
} else if (string.Compare(sourceFilePath, destFilePath, StringComparison.OrdinalIgnoreCase) == 0) {
} else if (PathUtils.IsSame(sourceFilePath, destFilePath)) {
// 路径仅大小写不同
Logger.Trace($"剪切文件到自身,但大小写不同:{sourceFilePath} → {destFilePath}");
Retrier.Attempt(delay: _ => TimeSpan.FromMilliseconds(200), isRetryAllowed: ex => ex is IOException, action: _ => {
+10 -7
View File
@@ -45,7 +45,7 @@ public static class PathUtils {
// 逐级向上寻找已存在的文件夹,将不存在的部分挪到 suffix,不再缩短
while (!DirectoryUtils.Exists(pathToShorten) && !FileUtils.Exists(pathToShorten)) { // 如果路径不存在
string? parentPath = Path.GetDirectoryName(pathToShorten);
if (string.IsNullOrEmpty(parentPath) || parentPath == pathToShorten) return pathName; // 已经到达根目录,全都不存在,直接返回
if (string.IsNullOrEmpty(parentPath) || PathUtils.IsSame(parentPath, pathToShorten)) return pathName; // 已经到达根目录,全都不存在,直接返回
pathToKeep = Path.Combine(PathUtils.GetLastPart(pathToShorten), pathToKeep);
pathToShorten = parentPath;
}
@@ -226,21 +226,24 @@ public static class PathUtils {
}
/// <summary>
/// 比较两个路径是否相同。
/// 比较两个路径是否相同。<c>null</c> 与空字符串视为相同的空路径。
/// </summary>
public static bool IsSame(string path1, string path2) {
return PathUtils.Compare(PathUtils.ForCompare(path1), PathUtils.ForCompare(path2)) == 0;
public static bool IsSame(string? path1, string? path2) {
return PathUtils.Compare(path1, path2) == 0;
}
/// <summary>
/// 比较两个路径,忽略大小写。
/// <para/><c>null</c> 与空字符串视为相同的空路径,空路径排在非空路径之前。
/// </summary>
/// <returns>
/// 小于 0:<paramref name="path1"/> 排在 <paramref name="path2"/> 之前。
/// <para/>等于 0:两个路径相等。
/// <para/>大于 0:<paramref name="path1"/> 排在 <paramref name="path2"/> 之后。
/// </returns>
public static int Compare(string path1, string path2) {
return string.Compare(PathUtils.ForCompare(path1), PathUtils.ForCompare(path2), true);
public static int Compare(string? path1, string? path2) {
if (string.IsNullOrEmpty(path1)) return string.IsNullOrEmpty(path2) ? 0 : -1;
if (string.IsNullOrEmpty(path2)) return 1;
return string.Compare(PathUtils.ForCompare(path1!), PathUtils.ForCompare(path2!), true);
}
/// <summary>
@@ -262,7 +265,7 @@ public static class PathUtils {
public static bool IsParentOf(string parentPath, string childPath) {
parentPath = PathUtils.ForCompare(parentPath);
childPath = PathUtils.ForCompare(childPath);
return childPath.StartsWithF(parentPath + @"\") || childPath == parentPath;
return childPath.StartsWithF(parentPath + @"\") || PathUtils.IsSame(childPath, parentPath);
}
}