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