fix: use external runtime app configuration

This commit is contained in:
zhengxuan.zhang
2026-08-11 10:47:48 +08:00
parent a4646b822b
commit 8bd0fb660a
4 changed files with 55 additions and 12 deletions
@@ -33,7 +33,11 @@ namespace XP.Common.Localization.Configs
{
try
{
var languageString = ConfigurationManager.AppSettings[LanguageKey];
// 显式读取当前进程对应的 exe 配置,避免 ConfigurationManager.AppSettings
// 在配置文件被设置页刷新后仍持有旧快照。
var config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
var languageString = config.AppSettings.Settings[LanguageKey]?.Value;
_logger.Info($"Reading language setting from configuration: {config.FilePath}; value={languageString ?? "<empty>"}");
if (string.IsNullOrWhiteSpace(languageString))
{
+1 -6
View File
@@ -101,12 +101,7 @@ namespace XplorePlane
// Telerik主题加载耗时需要在日志系统初始化后输出,暂存
var telerikThemeMs = sw.ElapsedMilliseconds;
// 强制使用中文 UI,确保 ImageProcessing 库显示中文
var zhCN = new CultureInfo("zh-CN");
Thread.CurrentThread.CurrentCulture = zhCN;
Thread.CurrentThread.CurrentUICulture = zhCN;
CultureInfo.DefaultThreadCurrentCulture = zhCN;
CultureInfo.DefaultThreadCurrentUICulture = zhCN;
// 语言由 ResxLocalizationService 根据配置初始化,不能在应用启动阶段强制覆盖为中文
// 配置 Serilog 日志系统(必须最先初始化,DumpService 依赖 Log.Logger
sw.Restart();
+41
View File
@@ -1,4 +1,6 @@
using System;
using System.Configuration;
using System.IO;
using Velopack;
namespace XplorePlane;
@@ -8,6 +10,7 @@ internal static class Program
[STAThread]
private static void Main()
{
ConfigureExternalAppConfig();
// Initialize Velopack at the real process entry point.
VelopackApp.Build().Run();
@@ -22,4 +25,42 @@ internal static class Program
app.InitializeComponent();
app.Run();
}
private static void ConfigureExternalAppConfig()
{
const string rootKey = "XpData:RootPath";
const string defaultRoot = @"D:\XPData";
try
{
var bootstrapPath = Path.Combine(AppContext.BaseDirectory, "App.config");
if (!File.Exists(bootstrapPath))
bootstrapPath = Path.Combine(AppContext.BaseDirectory, "XplorePlane.dll.config");
var rootPath = defaultRoot;
if (File.Exists(bootstrapPath))
{
var map = new ExeConfigurationFileMap { ExeConfigFilename = bootstrapPath };
var bootstrap = ConfigurationManager.OpenMappedExeConfiguration(map, ConfigurationUserLevel.None);
rootPath = bootstrap.AppSettings.Settings[rootKey]?.Value ?? defaultRoot;
}
if (!Path.IsPathRooted(rootPath))
rootPath = defaultRoot;
var externalDirectory = Path.Combine(rootPath, "runtime", "config");
var externalPath = Path.Combine(externalDirectory, "App.config");
Directory.CreateDirectory(externalDirectory);
if (!File.Exists(externalPath) && File.Exists(bootstrapPath))
File.Copy(bootstrapPath, externalPath);
if (File.Exists(externalPath))
AppDomain.CurrentDomain.SetData("APP_CONFIG_FILE", externalPath);
}
catch
{
// 配置重定向失败时继续使用宿主默认配置,避免阻止应用启动。
}
}
}
+8 -5
View File
@@ -202,8 +202,11 @@
<ProjectReference Include="..\XP.Calibration\XP.Calibration.csproj" />
<ProjectReference Include="..\XP.Scan\XP.Scan.csproj" />
</ItemGroup>
<Target Name="PostBuild" AfterTargets="PostBuildEvent">
<Exec Command="xcopy &quot;$(MSBuildProjectDirectory)\..\ExternalLibraries\*.*&quot; &quot;$(TargetDir)&quot; /d /y" />
<Exec Command="xcopy &quot;$(MSBuildProjectDirectory)\..\ExternalLibraries\Models&quot; &quot;$(TargetDir)Models\&quot; /d /y /i" />
</Target>
</Project>
<Target Name="PostBuild" AfterTargets="PostBuildEvent">
<Exec Command="xcopy &quot;$(MSBuildProjectDirectory)\..\ExternalLibraries\*.*&quot; &quot;$(TargetDir)&quot; /d /y" />
<Exec Command="xcopy &quot;$(MSBuildProjectDirectory)\..\ExternalLibraries\Models&quot; &quot;$(TargetDir)Models\&quot; /d /y /i" />
</Target>
<Target Name="RemoveGeneratedRuntimeConfig" AfterTargets="PostBuild;Publish">
<Delete Files="$(TargetDir)$(AssemblyName).dll.config" />
</Target>
</Project>