From 2a954ba52f461d3539851b1c145125c380f72aba Mon Sep 17 00:00:00 2001
From: LTCat <1043137532@qq.com>
Date: Fri, 9 Oct 2026 00:09:50 +0800
Subject: [PATCH] *
---
Shared/Utils/DirectoryUtils.cs | 6 +++---
Shared/Utils/FileUtils.cs | 4 ++--
Shared/Utils/PathUtils.cs | 17 ++++++++++-------
3 files changed, 15 insertions(+), 12 deletions(-)
diff --git a/Shared/Utils/DirectoryUtils.cs b/Shared/Utils/DirectoryUtils.cs
index 568dd8e..ca36bbf 100644
--- a/Shared/Utils/DirectoryUtils.cs
+++ b/Shared/Utils/DirectoryUtils.cs
@@ -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 {
///
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}");
diff --git a/Shared/Utils/FileUtils.cs b/Shared/Utils/FileUtils.cs
index b3dd7cb..7627978 100644
--- a/Shared/Utils/FileUtils.cs
+++ b/Shared/Utils/FileUtils.cs
@@ -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: _ => {
diff --git a/Shared/Utils/PathUtils.cs b/Shared/Utils/PathUtils.cs
index a0cf17a..4062d67 100644
--- a/Shared/Utils/PathUtils.cs
+++ b/Shared/Utils/PathUtils.cs
@@ -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 {
}
///
- /// 比较两个路径是否相同。
+ /// 比较两个路径是否相同。null 与空字符串视为相同的空路径。
///
- 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;
}
///
/// 比较两个路径,忽略大小写。
+ /// null 与空字符串视为相同的空路径,空路径排在非空路径之前。
///
///
/// 小于 0: 排在 之前。
/// 等于 0:两个路径相等。
/// 大于 0: 排在 之后。
///
- 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);
}
///
@@ -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);
}
}