所有配置文件统一放到 exe 同目录
- 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 通过,
This commit is contained in:
@@ -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` 在没有新文件时也会用旧位置兜底。
|
||||||
|
|
||||||
## 五、测试与验证
|
## 五、测试与验证
|
||||||
|
|
||||||
- 回归测试(纯内存,不需要目标程序):
|
- 回归测试(纯内存,不需要目标程序):
|
||||||
|
|||||||
@@ -64,7 +64,8 @@ namespace pacs_win_uia
|
|||||||
" --help 显示本帮助\r\n" +
|
" --help 显示本帮助\r\n" +
|
||||||
"\r\n" +
|
"\r\n" +
|
||||||
"默认路径:\r\n" +
|
"默认路径:\r\n" +
|
||||||
" 配置: %LocalAppData%\\pacs_win_uia\\control-monitors.json(界面“保存”的位置)\r\n" +
|
" 配置: <exe 目录>\\control-monitors.json(界面“保存”的位置;" +
|
||||||
|
"旧版本在 %LocalAppData%\\pacs_win_uia\\,首次读取会自动迁移)\r\n" +
|
||||||
" 输出: <exe 目录>\\out.json(从 bin\\Debug 运行就是 bin\\Debug\\out.json)\r\n" +
|
" 输出: <exe 目录>\\out.json(从 bin\\Debug 运行就是 bin\\Debug\\out.json)\r\n" +
|
||||||
"\r\n" +
|
"\r\n" +
|
||||||
"退出码: 0=全部命中;1=有未命中或取不到值;2=参数或配置错误";
|
"退出码: 0=全部命中;1=有未命中或取不到值;2=参数或配置错误";
|
||||||
@@ -385,12 +386,25 @@ namespace pacs_win_uia
|
|||||||
return allRead ? 0 : 1;
|
return allRead ? 0 : 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
// 默认配置文件路径:界面「保存」写到的位置(%LocalAppData%\pacs_win_uia\control-monitors.json)。
|
// 默认配置文件路径:与界面「保存」一致 —— **exe 同目录**的 control-monitors.json。
|
||||||
/// <summary>The config the GUI writes to by default.</summary>
|
// 旧版本的 %LocalAppData%\pacs_win_uia\control-monitors.json 仍然可读(还没迁移时兜底)。
|
||||||
|
/// <summary>The config the GUI writes to by default (next to the executable).</summary>
|
||||||
private static string GetDefaultConfigPath()
|
private static string GetDefaultConfigPath()
|
||||||
{
|
{
|
||||||
string directory = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);
|
string current = ControlMonitorStore.GetDefaultFilePath();
|
||||||
return Path.Combine(directory, "pacs_win_uia", "control-monitors.json");
|
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)。
|
// 默认输出路径:exe 同目录的 out.json(从 bin\Debug 运行就写到 bin\Debug)。
|
||||||
|
|||||||
@@ -5,6 +5,7 @@ using System.Runtime.Serialization;
|
|||||||
using System.Runtime.Serialization.Json;
|
using System.Runtime.Serialization.Json;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
using System.Xml;
|
using System.Xml;
|
||||||
|
using pacs_win_uia.Diagnostics;
|
||||||
using pacs_win_uia.Models;
|
using pacs_win_uia.Models;
|
||||||
|
|
||||||
namespace pacs_win_uia.Services
|
namespace pacs_win_uia.Services
|
||||||
@@ -45,11 +46,15 @@ namespace pacs_win_uia.Services
|
|||||||
private const int MaxFileBytes = ControlMonitorRules.MaxDocumentBytes;
|
private const int MaxFileBytes = ControlMonitorRules.MaxDocumentBytes;
|
||||||
// _filePath:已规范化(GetFullPath)的目标 JSON 文件绝对路径。
|
// _filePath:已规范化(GetFullPath)的目标 JSON 文件绝对路径。
|
||||||
private readonly string _filePath;
|
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()
|
public ControlMonitorStore()
|
||||||
: this(GetDefaultFilePath())
|
: this(GetDefaultFilePath())
|
||||||
{
|
{
|
||||||
|
_isDefaultPath = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
// 构造:指定文件路径;空路径直接抛异常。
|
// 构造:指定文件路径;空路径直接抛异常。
|
||||||
@@ -76,7 +81,7 @@ namespace pacs_win_uia.Services
|
|||||||
FileStream stream;
|
FileStream stream;
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
stream = new FileStream(FilePath, FileMode.Open, FileAccess.Read, FileShare.Read);
|
stream = new FileStream(ResolveReadPath(), FileMode.Open, FileAccess.Read, FileShare.Read);
|
||||||
}
|
}
|
||||||
catch (FileNotFoundException)
|
catch (FileNotFoundException)
|
||||||
{
|
{
|
||||||
@@ -240,15 +245,68 @@ namespace pacs_win_uia.Services
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetDefaultFilePath:返回默认文件路径(LocalApplicationData\pacs_win_uia\control-monitors.json)。
|
// 配置文件名(exe 同目录)。
|
||||||
private static string GetDefaultFilePath()
|
private const string FileName = "control-monitors.json";
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 默认配置文件路径:**exe 同目录**。与 settings.json、out.json、截图/OCR 输出目录一致,
|
||||||
|
/// 便于整包拷走、便于手改。exe 目录不可写时保存会失败并把路径写进日志/状态栏。
|
||||||
|
/// </summary>
|
||||||
|
public static string GetDefaultFilePath()
|
||||||
{
|
{
|
||||||
string directory = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);
|
string directory = Path.GetDirectoryName(typeof(ControlMonitorStore).Assembly.Location);
|
||||||
if (string.IsNullOrWhiteSpace(directory))
|
if (string.IsNullOrWhiteSpace(directory))
|
||||||
{
|
{
|
||||||
throw new InvalidOperationException("The local application data directory is unavailable.");
|
directory = Directory.GetCurrentDirectory();
|
||||||
|
}
|
||||||
|
|
||||||
|
return Path.Combine(directory, FileName);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 旧版本的位置(%LocalAppData%\pacs_win_uia\control-monitors.json)。只在默认路径
|
||||||
|
/// 还没有文件时用于一次性迁移,不再作为保存目标。取不到目录时返回 null。
|
||||||
|
/// </summary>
|
||||||
|
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 序列化器。
|
// CreateSerializer:创建 StoreDocument 的 DataContract JSON 序列化器。
|
||||||
|
|||||||
@@ -511,9 +511,8 @@ namespace pacs_win_uia.UI
|
|||||||
"配置文件位置:",
|
"配置文件位置:",
|
||||||
"· 设置:" + _store.PreferredPath,
|
"· 设置:" + _store.PreferredPath,
|
||||||
"· 设置(备用,exe 目录不可写时):" + _store.FallbackPath,
|
"· 设置(备用,exe 目录不可写时):" + _store.FallbackPath,
|
||||||
"· 监控定义:" + Path.Combine(
|
"· 监控定义:" + ControlMonitorStore.GetDefaultFilePath()
|
||||||
Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData),
|
+ "(旧版本在 %LocalAppData%\\pacs_win_uia\\,首次读取自动迁移到 exe 同目录)",
|
||||||
"pacs_win_uia", "control-monitors.json"),
|
|
||||||
"",
|
"",
|
||||||
"安全:默认只读;不注入目标进程、不修改内存、不绕过授权;命令以当前用户身份执行。",
|
"安全:默认只读;不注入目标进程、不修改内存、不绕过授权;命令以当前用户身份执行。",
|
||||||
"不要把患者信息放到命令行或上传到外部服务。"
|
"不要把患者信息放到命令行或上传到外部服务。"
|
||||||
|
|||||||
@@ -29,6 +29,8 @@ namespace PacsWinUia.RegressionTests
|
|||||||
tests.Add(new KeyValuePair<string, Action>("Store reads legacy UiaPath definitions", StoreLegacy));
|
tests.Add(new KeyValuePair<string, Action>("Store reads legacy UiaPath definitions", StoreLegacy));
|
||||||
// 防:设置缺省刷新间隔不再是 300ms,或缺失的区段未被补全。
|
// 防:设置缺省刷新间隔不再是 300ms,或缺失的区段未被补全。
|
||||||
tests.Add(new KeyValuePair<string, Action>("Settings missing refresh interval defaults to 300", SettingsDefault));
|
tests.Add(new KeyValuePair<string, Action>("Settings missing refresh interval defaults to 300", SettingsDefault));
|
||||||
|
// 防:默认配置文件又跑回 %LocalAppData%(需求:所有配置都在 exe 同级别)。
|
||||||
|
tests.Add(new KeyValuePair<string, Action>("Default monitor config lives next to the exe", DefaultConfigLocation));
|
||||||
// 防:悬浮条缺省启用被误判为显式停用(或反之)。
|
// 防:悬浮条缺省启用被误判为显式停用(或反之)。
|
||||||
tests.Add(new KeyValuePair<string, Action>("Floating JSON missing defaults differs from explicit disabled", FloatingDefaults));
|
tests.Add(new KeyValuePair<string, Action>("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()
|
private static void SettingsDefault()
|
||||||
{
|
{
|
||||||
using (var directory = new TemporaryDirectory())
|
using (var directory = new TemporaryDirectory())
|
||||||
|
|||||||
Reference in New Issue
Block a user