添加项目文件。
This commit is contained in:
25
Helpers/JsonConfigHelper.cs
Normal file
25
Helpers/JsonConfigHelper.cs
Normal file
@@ -0,0 +1,25 @@
|
||||
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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user