From 0ff787c5d9b4f43808099215192908589421fdad Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BE=99=E8=85=BE=E7=8C=AB=E8=B7=83?= <1043137532@qq.com> Date: Tue, 12 May 2026 16:52:42 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=87=E4=BB=B6=E6=93=8D=E4=BD=9C=E5=A4=A7?= =?UTF-8?q?=E5=B0=8F=E5=86=99=E5=85=BC=E5=AE=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Shared/Utils/DirectoryUtils.cs | 86 ++++++++++++++++++++++++---------- Shared/Utils/FileUtils.cs | 81 +++++++++++++++++++++++--------- Shared/Utils/PathUtils.cs | 11 +++++ 3 files changed, 130 insertions(+), 48 deletions(-) diff --git a/Shared/Utils/DirectoryUtils.cs b/Shared/Utils/DirectoryUtils.cs index 53fb8db..babd762 100644 --- a/Shared/Utils/DirectoryUtils.cs +++ b/Shared/Utils/DirectoryUtils.cs @@ -11,6 +11,15 @@ public static class DirectoryUtils { Directory.CreateDirectory(PathUtils.ForApi(path)); } + /// + /// 在临时文件夹下创建一个随机名称的文件夹,并返回其路径。 + /// + public static string CreateRandom() { + var tempDir = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName()); + DirectoryUtils.Create(tempDir); + return tempDir; + } + /// /// 判断文件夹是否存在。 /// @@ -54,48 +63,62 @@ public static class DirectoryUtils { /// /// 复制文件夹。 - /// 若原文件夹不存在,则不执行。 + /// 若复制自身到自身,则不执行操作;若仅大小写不同,则重命名此文件夹。 /// - public static void Copy(string sourceFolder, string destFolder, Action? progressHandler = null) { - if (!DirectoryUtils.Exists(sourceFolder)) { - Logger.Info($"尝试复制文件夹,但原文件夹不存在,已跳过复制:{sourceFolder} → {destFolder}"); - progressHandler?.Invoke(1); - return; - } + public static void Copy(string sourceFolder, string destFolder) { sourceFolder = PathUtils.ForCompare(sourceFolder); destFolder = PathUtils.ForCompare(destFolder); - int doneCount = 0; - Lazy totalCount = new(() => DirectoryUtils.GetFiles(sourceFolder).Count()); - Logger.Trace($"复制文件夹:{sourceFolder} → {destFolder}"); - foreach (var file in DirectoryUtils.GetFiles(sourceFolder)) { - FileUtils.Copy(file, file.Replace(sourceFolder, destFolder)); - doneCount++; - if (progressHandler is not null) progressHandler?.Invoke((double) doneCount / totalCount.Value); + if (string.Compare(sourceFolder, destFolder, ignoreCase: false) == 0) { + // 复制自身到自身,则不执行操作 + Logger.Trace($"复制文件夹到自身,不执行操作:{sourceFolder} → {destFolder}"); + } else if (string.Compare(sourceFolder, destFolder, ignoreCase: true) == 0) { + // 路径仅大小写不同,等效于重命名 + Logger.Trace($"复制文件夹到自身,但大小写不同,等效于重命名文件夹:{sourceFolder} → {destFolder}"); + DirectoryUtils.Move(sourceFolder, destFolder); + } else { + // 实际的复制 + Logger.Trace($"复制文件夹:{sourceFolder} → {destFolder}"); + foreach (var file in DirectoryUtils.GetFiles(sourceFolder)) FileUtils.Copy(file, file.Replace(sourceFolder, destFolder)); } } /// /// 剪切文件夹。 + /// 会创建对应文件夹、覆盖已有的文件夹。 /// public static void Move(string sourceFolder, string destFolder) { - if (!DirectoryUtils.Exists(sourceFolder)) return; - DirectoryUtils.Delete(destFolder); // Move 要求不存在对应文件夹 - Logger.Trace($"剪切文件夹:{sourceFolder} → {destFolder}"); - Directory.Move(PathUtils.ForApi(sourceFolder), PathUtils.ForApi(destFolder)); + SafetyCheck(sourceFolder); + sourceFolder = PathUtils.ForCompare(sourceFolder); + destFolder = PathUtils.ForCompare(destFolder); + if (string.Compare(sourceFolder, destFolder, ignoreCase: false) == 0) { + // 剪切自身到自身,则不执行操作 + Logger.Trace($"剪切文件夹到自身,不执行操作:{sourceFolder} → {destFolder}"); + } else if (string.Compare(sourceFolder, destFolder, ignoreCase: true) == 0) { + // 路径仅大小写不同 + Logger.Trace($"剪切文件夹到自身,但大小写不同:{sourceFolder} → {destFolder}"); + var temp = Path.Combine(PathUtils.RemoveLastPart(sourceFolder), Path.GetRandomFileName()); + DirectoryUtils.Move(sourceFolder, temp); + DirectoryUtils.Move(temp, destFolder); + } else if (PathUtils.GetDiskName(sourceFolder) == PathUtils.GetDiskName(destFolder)) { + // 同一磁盘剪切,直接调用 Move(这只修改文件夹名,效率更高) + Logger.Trace($"剪切文件夹到同一磁盘:{sourceFolder} → {destFolder}"); + DirectoryUtils.Delete(destFolder); // Move 要求此前不存在对应文件夹 + Directory.Move(PathUtils.ForApi(sourceFolder), PathUtils.ForApi(destFolder)); + } else { + // 不同磁盘,必须先复制再删除,这就是我们傻逼微软 + Logger.Trace($"剪切文件夹到不同磁盘:{sourceFolder} → {destFolder}"); + DirectoryUtils.Copy(sourceFolder, destFolder); + DirectoryUtils.Delete(sourceFolder); + } } /// /// 删除文件夹。 /// public static void Delete(string folder, bool toRecycleBin = false) { - // 安全检查 if (!DirectoryUtils.Exists(folder)) return; Logger.Trace($"{(toRecycleBin ? "将文件夹删除到回收站" : "删除文件夹")}:{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}"); + SafetyCheck(folder); // 删除 if (toRecycleBin) { FileUtils.DeleteToRecycleBin(folder); // 删除到回收站 @@ -110,7 +133,7 @@ public static class DirectoryUtils { } catch (DirectoryNotFoundException ex) { // #4549,也可能已被其他线程删除 if (DirectoryUtils.Exists(folder)) { Logger.Warn(ex, $"该文件夹可能为孤立的符号链接,尝试直接删除({folder})"); - Directory.Delete(folder, true); + Directory.Delete(folder); } else { throw; } @@ -118,11 +141,24 @@ public static class DirectoryUtils { } } } + + /// + /// 进行安全检查:若尝试操作桌面、文档、下载、Windows、当前程序所在文件夹,则抛出异常。 + /// + /// + private static void SafetyCheck(string 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}"); + } private static readonly Lazy> criticalFolders = new(() => new(new[] { PathUtils.CurrentFolder, Environment.GetFolderPath(Environment.SpecialFolder.Desktop), Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory), Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), + Environment.GetFolderPath(Environment.SpecialFolder.Windows), Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile), "Downloads") // 下载文件夹没有 SpecialFolder 枚举 }.Select(f => PathUtils.ForCompare(f)))); diff --git a/Shared/Utils/FileUtils.cs b/Shared/Utils/FileUtils.cs index 7c8069f..88f5eb5 100644 --- a/Shared/Utils/FileUtils.cs +++ b/Shared/Utils/FileUtils.cs @@ -74,8 +74,10 @@ public static class FileUtils { public static void Write(string filePath, byte[] content) { DirectoryUtils.Create(PathUtils.RemoveLastPart(filePath)); Logger.Trace($"写入文件:{filePath}({content.Length} 字节)"); - ResilientUtils.RetryOn(() - => File.WriteAllBytes(PathUtils.ForApi(filePath), content)); + ResilientUtils.RetryOn(() => { + FileUtils.Delete(filePath); + File.WriteAllBytes(PathUtils.ForApi(filePath), content); + }); } /// @@ -84,10 +86,13 @@ public static class FileUtils { /// 若文件已存在,则会覆盖原文件。 /// public static void Write(string filePath, Stream stream) { - using FileStream fileStream = FileUtils.CreateAsStream(filePath); - if (stream.CanSeek && stream.Position != 0) stream.Seek(0, SeekOrigin.Begin); - Logger.Trace($"写入文件:{filePath}({stream.GetType().Name}{(stream.CanSeek ? $" {stream.Length} 字节" : "")})"); - stream.CopyTo(fileStream); + ResilientUtils.RetryOn(() => { + FileUtils.Delete(filePath); + using FileStream fileStream = FileUtils.CreateAsStream(filePath); + if (stream.CanSeek && stream.Position != 0) stream.Seek(0, SeekOrigin.Begin); + Logger.Trace($"写入文件:{filePath}({stream.GetType().Name}{(stream.CanSeek ? $" {stream.Length} 字节" : "")})"); + stream.CopyTo(fileStream); + }); } /// /// 创建文件,并打开 。 @@ -96,10 +101,18 @@ public static class FileUtils { public static FileStream CreateAsStream(string filePath) { DirectoryUtils.Create(PathUtils.RemoveLastPart(filePath)); Logger.Trace($"创建文件流:{filePath}"); - return ResilientUtils.RetryOn(() - => new FileStream(PathUtils.ForApi(filePath), FileMode.Create)); + return ResilientUtils.RetryOn(() => { + FileUtils.Delete(filePath); + return new FileStream(PathUtils.ForApi(filePath), FileMode.Create); + }); } + /// + /// 在临时文件夹下创建一个随机名称的文件,并返回其路径。 + /// + public static string CreateRandom() + => ResilientUtils.RetryOn(Path.GetTempFileName); + #endregion #region 复制 / 剪切 @@ -107,18 +120,25 @@ public static class FileUtils { /// /// 复制文件。 /// 会创建对应文件夹、覆盖已有的文件。 - /// 若原文件不存在,则不执行。 + /// 若复制自身到自身,则不执行操作;若仅大小写不同,则重命名此文件。 /// public static void Copy(string sourceFilePath, string destFilePath) { - if (sourceFilePath == destFilePath) return; // 如果复制同一个文件则跳过 - if (!FileUtils.Exists(sourceFilePath)) { - Logger.Info($"尝试复制文件,但原文件不存在,已跳过复制:{sourceFilePath} → {destFilePath}"); - return; + sourceFilePath = PathUtils.ForCompare(sourceFilePath); + destFilePath = PathUtils.ForCompare(destFilePath); + if (string.Compare(sourceFilePath, destFilePath, ignoreCase:false) == 0) { + // 复制自身到自身,则不执行操作 + Logger.Trace($"复制文件到自身,不执行操作:{sourceFilePath} → {destFilePath}"); + } else if (string.Compare(sourceFilePath, destFilePath, ignoreCase:true) == 0) { + // 路径仅大小写不同,等效于重命名 + Logger.Trace($"复制文件到自身,但大小写不同,等效于重命名文件:{sourceFilePath} → {destFilePath}"); + FileUtils.Move(sourceFilePath, destFilePath); + } else { + // 实际的复制 + DirectoryUtils.Create(PathUtils.RemoveLastPart(destFilePath)); + Logger.Trace($"复制文件:{sourceFilePath} → {destFilePath}"); + ResilientUtils.RetryOn(() + => File.Copy(PathUtils.ForApi(sourceFilePath), PathUtils.ForApi(destFilePath), true)); } - DirectoryUtils.Create(PathUtils.RemoveLastPart(destFilePath)); - Logger.Trace($"复制文件:{sourceFilePath} → {destFilePath}"); - ResilientUtils.RetryOn(() - => File.Copy(PathUtils.ForApi(sourceFilePath), PathUtils.ForApi(destFilePath), true)); } /// @@ -126,12 +146,27 @@ public static class FileUtils { /// 会创建对应文件夹、覆盖已有的文件。 /// public static void Move(string sourceFilePath, string destFilePath) { - if (sourceFilePath == destFilePath) return; // 如果移动同一个文件则跳过 - DirectoryUtils.Create(PathUtils.RemoveLastPart(destFilePath)); - FileUtils.Delete(destFilePath); - Logger.Trace($"剪切文件:{sourceFilePath} → {destFilePath}"); - ResilientUtils.RetryOn(() - => File.Move(PathUtils.ForApi(sourceFilePath), PathUtils.ForApi(destFilePath))); + sourceFilePath = PathUtils.ForCompare(sourceFilePath); + destFilePath = PathUtils.ForCompare(destFilePath); + if (string.Compare(sourceFilePath, destFilePath, ignoreCase: false) == 0) { + // 剪切自身到自身,则不执行操作 + Logger.Trace($"剪切文件到自身,不执行操作:{sourceFilePath} → {destFilePath}"); + } else if (string.Compare(sourceFilePath, destFilePath, ignoreCase: true) == 0) { + // 路径仅大小写不同 + Logger.Trace($"剪切文件到自身,但大小写不同:{sourceFilePath} → {destFilePath}"); + ResilientUtils.RetryOn(() => { + var temp = Path.Combine(PathUtils.RemoveLastPart(sourceFilePath), Path.GetRandomFileName()); + File.Move(PathUtils.ForApi(sourceFilePath), PathUtils.ForApi(temp)); + File.Move(PathUtils.ForApi(temp), PathUtils.ForApi(destFilePath)); + }); + } else { + // 实际的剪切 + DirectoryUtils.Create(PathUtils.RemoveLastPart(destFilePath)); + FileUtils.Delete(destFilePath); + Logger.Trace($"剪切文件:{sourceFilePath} → {destFilePath}"); + ResilientUtils.RetryOn(() + => File.Move(PathUtils.ForApi(sourceFilePath), PathUtils.ForApi(destFilePath))); + } } #endregion diff --git a/Shared/Utils/PathUtils.cs b/Shared/Utils/PathUtils.cs index 890320b..27ab600 100644 --- a/Shared/Utils/PathUtils.cs +++ b/Shared/Utils/PathUtils.cs @@ -146,6 +146,17 @@ public static class PathUtils { return name.Contains(".") ? name.AfterLast(".").Lower() : ""; } + /// + /// 获取路径的盘符,返回一个大写字母。 + /// 若路径不包含盘符,则返回 null。 + /// + public static char? GetDiskName(string? path) { + if (string.IsNullOrEmpty(path)) return null; + path = PathUtils.WithoutLongPath(path!); + if (path.StartsWithF(@"\\") || path.StartsWithF("/")) return null; + return char.ToUpper(path[0]); + } + /// /// 统一路径格式,以便比较。 /// 具体而言:将短路径展开,去除前导的 \\?\,将分隔符改为 \,去除末尾的分隔符。