53 lines
1.5 KiB
C#
53 lines
1.5 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.IO;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Text.Json;
|
|
using System.Threading.Tasks;
|
|
using Wpf_AiSportsMicrospace.Dto;
|
|
|
|
namespace Wpf_AiSportsMicrospace.Service
|
|
{
|
|
/// <summary>
|
|
/// 配置服务
|
|
/// </summary>
|
|
public class ConfigService
|
|
{
|
|
public Dictionary<string, ConfigSet> ConfigDic { get; set; } = new Dictionary<string, ConfigSet>();
|
|
|
|
public ConfigService()
|
|
{
|
|
LoadAllConfigs();
|
|
}
|
|
|
|
/// <summary>
|
|
/// 保存配置信息
|
|
/// </summary>
|
|
/// <param name="filePath"></param>
|
|
public void SaveAllConfigs(string fileName = "configs.json")
|
|
{
|
|
string basePath = AppContext.BaseDirectory; // 当前运行目录
|
|
string filePath = Path.Combine(basePath, fileName);
|
|
|
|
var json = JsonSerializer.Serialize(ConfigDic, new JsonSerializerOptions { WriteIndented = true });
|
|
File.WriteAllText(filePath, json);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 加载配置信息
|
|
/// </summary>
|
|
/// <param name="fileName"></param>
|
|
public void LoadAllConfigs(string fileName = "configs.json")
|
|
{
|
|
string basePath = AppContext.BaseDirectory;
|
|
string filePath = Path.Combine(basePath, fileName);
|
|
|
|
if (!File.Exists(filePath)) return;
|
|
|
|
var json = File.ReadAllText(filePath);
|
|
ConfigDic = JsonSerializer.Deserialize<Dictionary<string, ConfigSet>>(json);
|
|
}
|
|
}
|
|
}
|