From 98157d65c7627721c816c02e8be96ac4fe61d615 Mon Sep 17 00:00:00 2001 From: LTCat <1043137532@qq.com> Date: Sat, 17 Oct 2026 04:13:58 +0800 Subject: [PATCH] * --- Shared/General/Config.cs | 1 - Shared/Utils/FileUtils.cs | 13 +++++-------- 2 files changed, 5 insertions(+), 9 deletions(-) diff --git a/Shared/General/Config.cs b/Shared/General/Config.cs index a039fcd..5b31d83 100644 --- a/Shared/General/Config.cs +++ b/Shared/General/Config.cs @@ -231,7 +231,6 @@ public static class ConfigUtils { try { return FileUtils.Exists(filePath) ? JObject.Parse(FileUtils.ReadAsString(filePath)) : []; } catch { - // JSON 异常可能包含凭据内容,不保留原始异常,也不回退空配置。 throw new InvalidDataException($"读取密钥文件失败({filePath}),请检查文件内容与加密格式。"); } }); diff --git a/Shared/Utils/FileUtils.cs b/Shared/Utils/FileUtils.cs index 486c879..4e9a547 100644 --- a/Shared/Utils/FileUtils.cs +++ b/Shared/Utils/FileUtils.cs @@ -105,14 +105,12 @@ public static class FileUtils { } /// - /// 原子写入文件。成功时目标文件完整切换为新内容,失败时已有目标文件保持不变。 - /// 不提供并发写入合并保证。 + /// 原子写入文件。写入失败时目标文件保持不变。 /// public static void WriteAtomic(string filePath, string text, Encoding? encoding = null) => FileUtils.WriteAtomic(filePath, (encoding ?? new UTF8Encoding()).GetBytes(text)); /// - /// 原子写入文件。成功时目标文件完整切换为新内容,失败时已有目标文件保持不变。 - /// 不提供并发写入合并保证。 + /// 原子写入文件。写入失败时目标文件保持不变。 /// public static void WriteAtomic(string filePath, byte[] content) { string tempPath = $"{filePath}.{Guid.NewGuid():N}.tmp"; @@ -462,14 +460,13 @@ public static class FileUtils { => File.Exists(PathUtils.ForApi(filePath)); /// - /// 检查文件是否未被其他进程占用 - /// 被占用或无权读取时返回 。 + /// 检查文件是否存在,且当前程序有独占的读写权限,未被其他进程占用。 /// public static bool IsAvailable(string filePath) { - if (!Exists(filePath)) return true; + if (!Exists(filePath)) return false; try { Retrier.Attempt(maxAttempts: 3, delay: _ => TimeSpan.FromMilliseconds(200), isRetryAllowed: ex => ex is IOException, action: _ => { - using var stream = new FileStream(PathUtils.ForApi(filePath), FileMode.Open, FileAccess.Read, FileShare.None); + using var unused = new FileStream(PathUtils.ForApi(filePath), FileMode.Open, FileAccess.ReadWrite, FileShare.None); }); return true; } catch (Exception ex) when (ex is IOException or UnauthorizedAccessException) {