From 53ed2570725a98504920e66fbdfecf66c3696ad5 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, 28 Apr 2026 00:55:41 +0800 Subject: [PATCH] =?UTF-8?q?ref:=20FileUtils=20=E4=B8=AD=E7=9A=84=E6=96=87?= =?UTF-8?q?=E4=BB=B6=E5=86=99=E5=85=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Extensions/StringExtensions.cs | 4 +-- General/MsDelta.cs | 4 +-- Utils/DirectoryUtils.cs | 6 ++-- Utils/{PathUtils.cs => FileUtils.cs} | 42 ++++++++++++++++++++++++++-- 4 files changed, 48 insertions(+), 8 deletions(-) rename Utils/{PathUtils.cs => FileUtils.cs} (56%) diff --git a/Extensions/StringExtensions.cs b/Extensions/StringExtensions.cs index f1e8015..2f1708b 100644 --- a/Extensions/StringExtensions.cs +++ b/Extensions/StringExtensions.cs @@ -168,7 +168,7 @@ public static class StringExtensions { /// 将字符串中的换行符统一替换为指定字符。 /// 若指定了 ,会将多次换行合并成一次换行。 /// - public static string ReplaceLineEndings([DisallowNull] this string input, string newValue, bool mergeMultiple = false) => + public static string ReplaceLineEndings(this string input, string newValue, bool mergeMultiple = false) => Regex.Replace(input, mergeMultiple ? @"(?:\r\n|[\n\r\f\u0085\u2028\u2029])+" : @"\r\n|[\n\r\f\u0085\u2028\u2029]", newValue.Replace("$", "$$"), // 避免识别成捕获组 @@ -196,6 +196,6 @@ public static class StringExtensions { /// /// 该字符串中的字符是否均为 ASCII 字符。 /// - public static bool IsAsciiOnly([DisallowNull] this string input) => input.All(c => c < 128); + public static bool IsAsciiOnly(this string input) => input.All(c => c < 128); } diff --git a/General/MsDelta.cs b/General/MsDelta.cs index 119040a..1fc6348 100644 --- a/General/MsDelta.cs +++ b/General/MsDelta.cs @@ -17,7 +17,7 @@ public static class MsDelta { // fileTypeSet:https://learn.microsoft.com/en-us/previous-versions/bb417345(v=msdn.10)?redirectedfrom=MSDN#file-type-sets (15L: DELTA_FILE_TYPE_SET_EXECUTABLES) // 131072L: IgnoreFileSizeLimit, 32u: Crc32 if (!CreateDelta(15L, 131072L, 0L, - PathUtils.Shorten(oldFilePath), PathUtils.Shorten(newFilePath), null, null, new DeltaInput(), IntPtr.Zero, 32u, PathUtils.Shorten(deltaFilePath))) + FileUtils.ShortenPath(oldFilePath), FileUtils.ShortenPath(newFilePath), null, null, new DeltaInput(), IntPtr.Zero, 32u, FileUtils.ShortenPath(deltaFilePath))) throw new Win32Exception(); } @@ -31,7 +31,7 @@ public static class MsDelta { if (!File.Exists(deltaFilePath)) throw new FileNotFoundException($"补丁文件不存在:{deltaFilePath}", deltaFilePath); DirectoryUtils.Create(newFilePath, isFilePath: true); // 1L: AllowLegacy - if (!ApplyDelta(1L, PathUtils.Shorten(oldFilePath), PathUtils.Shorten(deltaFilePath), PathUtils.Shorten(newFilePath))) + if (!ApplyDelta(1L, FileUtils.ShortenPath(oldFilePath), FileUtils.ShortenPath(deltaFilePath), FileUtils.ShortenPath(newFilePath))) throw new Win32Exception(); } diff --git a/Utils/DirectoryUtils.cs b/Utils/DirectoryUtils.cs index 9731994..64a3d7e 100644 --- a/Utils/DirectoryUtils.cs +++ b/Utils/DirectoryUtils.cs @@ -13,11 +13,13 @@ public static class DirectoryUtils { /// /// 创建文件夹,或文件所在的文件夹。 - /// 支持长路径,文件夹已存在时不会抛出异常。 + /// 支持长路径。 + /// 文件夹已存在时不会抛出异常。 /// public static void Create(string path, bool isFilePath = false) { + path = FileUtils.ShortenPath(path); if (isFilePath) path = Path.GetDirectoryName(path); - Directory.CreateDirectory(PathUtils.Shorten(path)); + Directory.CreateDirectory(path); } } diff --git a/Utils/PathUtils.cs b/Utils/FileUtils.cs similarity index 56% rename from Utils/PathUtils.cs rename to Utils/FileUtils.cs index 9d211a4..da5769a 100644 --- a/Utils/PathUtils.cs +++ b/Utils/FileUtils.cs @@ -1,11 +1,47 @@ namespace MeloongCore; -public static class PathUtils { +public static class FileUtils { + + #region 写入 + + /// + /// 写入文件。 + /// 如果文件或文件夹不存在,则会自动创建。 + /// + public static void Write(string filePath, string text, Encoding? encoding = null) { + DirectoryUtils.Create(filePath, isFilePath: true); + File.WriteAllText(filePath, text, encoding ?? new UTF8Encoding()); + } + + /// + /// 写入文件。 + /// 如果文件或文件夹不存在,则会自动创建。 + /// + public static void Write(string filePath, byte[] content) { + DirectoryUtils.Create(filePath, isFilePath: true); + File.WriteAllBytes(filePath, content); + } + + /// + /// 将 写入文件。 + /// 会将流的位置主动重置到开头。 + /// 如果文件或文件夹不存在,则会自动创建。 + /// + public static void Write(string filePath, Stream stream) { + DirectoryUtils.Create(filePath, isFilePath: true); + using FileStream fileStream = new(filePath, FileMode.Create, FileAccess.Write); + if (stream.CanSeek && stream.Position != 0) stream.Seek(0, SeekOrigin.Begin); + stream.CopyTo(fileStream); + } + + #endregion + + #region 短路径 /// /// 若路径较长,则尽量将其转换为短路径。 /// 若输入的是文件夹路径,不保证其结尾是否有 \ 。 /// - public static string Shorten(string fullName, bool keepFileName = false) { + public static string ShortenPath(string fullName, bool keepFileName = false) { if (string.IsNullOrEmpty(fullName) || fullName.Length <= 200) return fullName; fullName = fullName.Replace('/', '\\'); @@ -35,4 +71,6 @@ public static class PathUtils { [DllImport("kernel32.dll", CharSet = CharSet.Unicode)] private static extern int GetShortPathNameW(string lpszLongPath, StringBuilder lpszShortPath, int cchBuffer); + #endregion + }