25 lines
777 B
C#
25 lines
777 B
C#
|
|
using ASTM_D7896_Tester.Models;
|
|||
|
|
using System.IO;
|
|||
|
|
using System.Text.Json;
|
|||
|
|
|
|||
|
|
namespace ASTM_D7896_Tester.Helpers;
|
|||
|
|
|
|||
|
|
public static class JsonConfigHelper
|
|||
|
|
{
|
|||
|
|
private static readonly string ConfigPath = "appsettings.json";
|
|||
|
|
|
|||
|
|
public static AppConfig LoadConfig()
|
|||
|
|
{
|
|||
|
|
if (!File.Exists(ConfigPath))
|
|||
|
|
throw new FileNotFoundException($"配置文件 {ConfigPath} 未找到。");
|
|||
|
|
var json = File.ReadAllText(ConfigPath);
|
|||
|
|
return JsonSerializer.Deserialize<AppConfig>(json) ?? new AppConfig();
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
public static void SaveConfig(AppConfig config)
|
|||
|
|
{
|
|||
|
|
var options = new JsonSerializerOptions { WriteIndented = true };
|
|||
|
|
var json = JsonSerializer.Serialize(config, options);
|
|||
|
|
File.WriteAllText(ConfigPath, json);
|
|||
|
|
}
|
|||
|
|
}
|