Skip to content

Commit

Permalink
🐛 修复修改配置路径,重启后失效的问题
Browse files Browse the repository at this point in the history
  • Loading branch information
K12f committed Sep 24, 2024
1 parent b7dd3d0 commit 3b1dd7d
Showing 1 changed file with 12 additions and 7 deletions.
19 changes: 12 additions & 7 deletions src/BlueCatKoKo.Ui/Services/AppConfigService.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System.Configuration;
using System.IO;
using BlueCatKoKo.Ui.Models;
using Newtonsoft.Json;
Expand Down Expand Up @@ -27,21 +28,25 @@ private static string Absolute(string relativePath)
return Path.Combine(StartUpPath, relativePath);
}

public void Write(AppConfig config)
public void Write(AppConfig appConfig)
{
_rwLock.EnterWriteLock();
try
{
var path = Absolute(config.DownloadPath ?? "./");
var path = Absolute(appConfig.DownloadPath ?? "./");
if (!Directory.Exists(path)) Directory.CreateDirectory(path);

var file = Path.Combine(StartUpPath, _appSettingsFile);
_logger.Information("保存配置文件:{file}", file);
_logger.Information("保存配置内容:{@config}", config);
var appSettings = File.ReadAllText(file);
var appSettingsObj = JObject.Parse(appSettings);
appSettingsObj[nameof(AppConfig)] = JsonConvert.SerializeObject(config);
File.WriteAllText(file, appSettingsObj.ToString());
_logger.Information("保存配置内容:{@config}", appConfig);
var appSettingsJson = File.ReadAllText(file);
var appSettings = JsonConvert.DeserializeObject<AppSetting>(appSettingsJson);
if (appSettings is null)
{
throw new ConfigurationErrorsException("解析配置文件失败");
}
appSettings.AppConfig = appConfig;
File.WriteAllText(file, JsonConvert.SerializeObject(appSettings));
}
catch (Exception e)
{
Expand Down

0 comments on commit 3b1dd7d

Please sign in to comment.