From 44fb305dc490efc91f54ea6b8bdcad3e0211aa29 Mon Sep 17 00:00:00 2001 From: zimoyin <2556608754@qq.com> Date: Tue, 13 Oct 2026 16:18:57 +0800 Subject: [PATCH] =?UTF-8?q?=E6=89=80=E6=9C=89=E9=85=8D=E7=BD=AE=E6=96=87?= =?UTF-8?q?=E4=BB=B6=E7=BB=9F=E4=B8=80=E6=94=BE=E5=88=B0=20exe=20=E5=90=8C?= =?UTF-8?q?=E7=9B=AE=E5=BD=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - control-monitors.json 从 %LocalAppData%\pacs_win_uia\ 改到 exe 同目录 (settings.json / out.json / 截图与 OCR 目录本来就是 exe 同目录,现在一致) - 首次读取时把旧位置的监控配置复制过来(只复制不删除),并记日志; exe 目录不可写时退化为直接读旧文件,保存会明确报错,不会丢配置 - ControlMonitorStore.GetDefaultFilePath()/GetLegacyFilePath() 公开, Program 的 --read-saved 与设置页「关于」都改用同一套路径 - 回归新增用例:默认配置与设置文件都必须在 exe 同目录、旧路径与当前路径必须不同 - 实测(隔离宿主):默认路径=exe 同目录;旧位置有 1 条配置被复制过来并能正常读出 测试 24/24 通过, --- docs/代码导览.md | 10 +++ src/pacs_win_uia/Program.cs | 24 +++++-- .../Services/ControlMonitorStore.cs | 72 +++++++++++++++++-- src/pacs_win_uia/UI/SettingsForm.cs | 5 +- tests/regression/PersistenceTests.cs | 26 +++++++ 5 files changed, 122 insertions(+), 15 deletions(-) diff --git a/docs/代码导览.md b/docs/代码导览.md index 770f50a..22ba9e5 100644 --- a/docs/代码导览.md +++ b/docs/代码导览.md @@ -101,6 +101,16 @@ MainForm(UI/MainForm.cs) 三栏主界面:窗口列表 | 控件树/列 --- +## 四·五、配置文件位置(都在 exe 同目录) + +| 文件 | 用途 | +|---|---| +| `control-monitors.json` | 控件监控定义(界面「保存」写入) | +| `settings.json` | 应用设置(界面、悬浮条、命令、OCR…) | +| `out.json` | `--read-saved` 的默认输出 | + +旧版本的 `control-monitors.json` 在 `%LocalAppData%\pacs_win_uia\`:**首次读取会自动复制到 exe 同目录**(原文件保留不动);如果 exe 目录不可写,则退化为直接读旧位置,保存时会明确报错。`--read-saved` 在没有新文件时也会用旧位置兜底。 + ## 五、测试与验证 - 回归测试(纯内存,不需要目标程序): diff --git a/src/pacs_win_uia/Program.cs b/src/pacs_win_uia/Program.cs index dbe3ac3..be55adc 100644 --- a/src/pacs_win_uia/Program.cs +++ b/src/pacs_win_uia/Program.cs @@ -64,7 +64,8 @@ namespace pacs_win_uia " --help 显示本帮助\r\n" + "\r\n" + "默认路径:\r\n" + - " 配置: %LocalAppData%\\pacs_win_uia\\control-monitors.json(界面“保存”的位置)\r\n" + + " 配置: \\control-monitors.json(界面“保存”的位置;" + + "旧版本在 %LocalAppData%\\pacs_win_uia\\,首次读取会自动迁移)\r\n" + " 输出: \\out.json(从 bin\\Debug 运行就是 bin\\Debug\\out.json)\r\n" + "\r\n" + "退出码: 0=全部命中;1=有未命中或取不到值;2=参数或配置错误"; @@ -385,12 +386,25 @@ namespace pacs_win_uia return allRead ? 0 : 1; } - // 默认配置文件路径:界面「保存」写到的位置(%LocalAppData%\pacs_win_uia\control-monitors.json)。 - /// The config the GUI writes to by default. + // 默认配置文件路径:与界面「保存」一致 —— **exe 同目录**的 control-monitors.json。 + // 旧版本的 %LocalAppData%\pacs_win_uia\control-monitors.json 仍然可读(还没迁移时兜底)。 + /// The config the GUI writes to by default (next to the executable). private static string GetDefaultConfigPath() { - string directory = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData); - return Path.Combine(directory, "pacs_win_uia", "control-monitors.json"); + string current = ControlMonitorStore.GetDefaultFilePath(); + if (File.Exists(current)) + { + return current; + } + + string legacy = ControlMonitorStore.GetLegacyFilePath(); + if (legacy != null && File.Exists(legacy)) + { + Console.Error.WriteLine("默认位置没有配置,改用旧位置:" + legacy); + return legacy; + } + + return current; } // 默认输出路径:exe 同目录的 out.json(从 bin\Debug 运行就写到 bin\Debug)。 diff --git a/src/pacs_win_uia/Services/ControlMonitorStore.cs b/src/pacs_win_uia/Services/ControlMonitorStore.cs index f172ea5..75fa373 100644 --- a/src/pacs_win_uia/Services/ControlMonitorStore.cs +++ b/src/pacs_win_uia/Services/ControlMonitorStore.cs @@ -5,6 +5,7 @@ using System.Runtime.Serialization; using System.Runtime.Serialization.Json; using System.Text; using System.Xml; +using pacs_win_uia.Diagnostics; using pacs_win_uia.Models; namespace pacs_win_uia.Services @@ -45,11 +46,15 @@ namespace pacs_win_uia.Services private const int MaxFileBytes = ControlMonitorRules.MaxDocumentBytes; // _filePath:已规范化(GetFullPath)的目标 JSON 文件绝对路径。 private readonly string _filePath; + // _isDefaultPath:只有默认路径才做旧位置迁移;显式传入的路径按原样使用。 + private bool _isDefaultPath; - // 无参构造:使用默认路径(LocalApplicationData\pacs_win_uia\control-monitors.json)。 + // 无参构造:使用默认路径(**exe 同目录**的 control-monitors.json)。 + // 旧版本把它放在 %LocalAppData%\pacs_win_uia\,首次读取时做一次性迁移(见 ResolveReadPath)。 public ControlMonitorStore() : this(GetDefaultFilePath()) { + _isDefaultPath = true; } // 构造:指定文件路径;空路径直接抛异常。 @@ -76,7 +81,7 @@ namespace pacs_win_uia.Services FileStream stream; try { - stream = new FileStream(FilePath, FileMode.Open, FileAccess.Read, FileShare.Read); + stream = new FileStream(ResolveReadPath(), FileMode.Open, FileAccess.Read, FileShare.Read); } catch (FileNotFoundException) { @@ -240,15 +245,68 @@ namespace pacs_win_uia.Services } } - // GetDefaultFilePath:返回默认文件路径(LocalApplicationData\pacs_win_uia\control-monitors.json)。 - private static string GetDefaultFilePath() + // 配置文件名(exe 同目录)。 + private const string FileName = "control-monitors.json"; + + /// + /// 默认配置文件路径:**exe 同目录**。与 settings.json、out.json、截图/OCR 输出目录一致, + /// 便于整包拷走、便于手改。exe 目录不可写时保存会失败并把路径写进日志/状态栏。 + /// + public static string GetDefaultFilePath() { - string directory = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData); + string directory = Path.GetDirectoryName(typeof(ControlMonitorStore).Assembly.Location); if (string.IsNullOrWhiteSpace(directory)) { - throw new InvalidOperationException("The local application data directory is unavailable."); + directory = Directory.GetCurrentDirectory(); + } + + return Path.Combine(directory, FileName); + } + + /// + /// 旧版本的位置(%LocalAppData%\pacs_win_uia\control-monitors.json)。只在默认路径 + /// 还没有文件时用于一次性迁移,不再作为保存目标。取不到目录时返回 null。 + /// + public static string GetLegacyFilePath() + { + string directory = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData); + return string.IsNullOrWhiteSpace(directory) ? null : Path.Combine(directory, "pacs_win_uia", FileName); + } + + // ResolveReadPath:决定这次 Load 从哪个文件读。 + // · 显式路径:按调用方给的文件读(不存在就让 Load 返回空列表)。 + // · 默认路径:优先 exe 同目录;文件不存在而旧位置有,就把旧文件复制过来(一次性迁移), + // 复制失败(比如 exe 目录只读)则退化为直接读旧文件,绝不丢配置。 + private string ResolveReadPath() + { + if (File.Exists(_filePath) || !_isDefaultPath) + { + return _filePath; + } + + string legacy = GetLegacyFilePath(); + if (legacy == null || !File.Exists(legacy)) + { + return _filePath; + } + + try + { + string directory = Path.GetDirectoryName(_filePath); + if (!string.IsNullOrEmpty(directory)) + { + Directory.CreateDirectory(directory); + } + + File.Copy(legacy, _filePath, false); + AppLog.Info("监控配置已从旧位置迁移到 exe 同目录:" + legacy + " -> " + _filePath); + return _filePath; + } + catch (Exception ex) + { + AppLog.Warn("迁移监控配置到 exe 同目录失败,本次仍从旧位置读取:" + ex.Message); + return legacy; } - return Path.Combine(directory, "pacs_win_uia", "control-monitors.json"); } // CreateSerializer:创建 StoreDocument 的 DataContract JSON 序列化器。 diff --git a/src/pacs_win_uia/UI/SettingsForm.cs b/src/pacs_win_uia/UI/SettingsForm.cs index 98a851a..f76bbea 100644 --- a/src/pacs_win_uia/UI/SettingsForm.cs +++ b/src/pacs_win_uia/UI/SettingsForm.cs @@ -511,9 +511,8 @@ namespace pacs_win_uia.UI "配置文件位置:", "· 设置:" + _store.PreferredPath, "· 设置(备用,exe 目录不可写时):" + _store.FallbackPath, - "· 监控定义:" + Path.Combine( - Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), - "pacs_win_uia", "control-monitors.json"), + "· 监控定义:" + ControlMonitorStore.GetDefaultFilePath() + + "(旧版本在 %LocalAppData%\\pacs_win_uia\\,首次读取自动迁移到 exe 同目录)", "", "安全:默认只读;不注入目标进程、不修改内存、不绕过授权;命令以当前用户身份执行。", "不要把患者信息放到命令行或上传到外部服务。" diff --git a/tests/regression/PersistenceTests.cs b/tests/regression/PersistenceTests.cs index ee410dc..66d6171 100644 --- a/tests/regression/PersistenceTests.cs +++ b/tests/regression/PersistenceTests.cs @@ -29,6 +29,8 @@ namespace PacsWinUia.RegressionTests tests.Add(new KeyValuePair("Store reads legacy UiaPath definitions", StoreLegacy)); // 防:设置缺省刷新间隔不再是 300ms,或缺失的区段未被补全。 tests.Add(new KeyValuePair("Settings missing refresh interval defaults to 300", SettingsDefault)); + // 防:默认配置文件又跑回 %LocalAppData%(需求:所有配置都在 exe 同级别)。 + tests.Add(new KeyValuePair("Default monitor config lives next to the exe", DefaultConfigLocation)); // 防:悬浮条缺省启用被误判为显式停用(或反之)。 tests.Add(new KeyValuePair("Floating JSON missing defaults differs from explicit disabled", FloatingDefaults)); } @@ -126,6 +128,30 @@ namespace PacsWinUia.RegressionTests } } + + // 默认配置必须在 exe 同目录;旧位置只作为一次性迁移来源保留。 + // 这里只断言路径本身,不读写磁盘——绝不能在测试里碰用户真实的配置文件。 + private static void DefaultConfigLocation() + { + string expectedDirectory = Path.GetDirectoryName(typeof(ControlMonitorStore).Assembly.Location); + string defaultPath = ControlMonitorStore.GetDefaultFilePath(); + Check.Equal(expectedDirectory, Path.GetDirectoryName(defaultPath), "Default config is not next to the exe"); + Check.Equal("control-monitors.json", Path.GetFileName(defaultPath), "Default config file name"); + Check.True(Path.IsPathRooted(defaultPath), "Default config path must be absolute"); + + string preferred = new AppSettingsStore().PreferredPath; + Check.Equal(expectedDirectory, Path.GetDirectoryName(preferred), "Settings file is not next to the exe"); + + string legacy = ControlMonitorStore.GetLegacyFilePath(); + if (legacy != null) + { + Check.True(!string.Equals(legacy, defaultPath, StringComparison.OrdinalIgnoreCase), + "Legacy and current config paths must differ"); + Check.True(legacy.IndexOf("pacs_win_uia", StringComparison.OrdinalIgnoreCase) >= 0, + "Legacy path should stay inside pacs_win_uia"); + } + } + private static void SettingsDefault() { using (var directory = new TemporaryDirectory())