using Newtonsoft.Json; using System; using System.IO; using System.Collections.Concurrent; namespace HBLConsole.Service { /// /// Json参数服务类 /// public class Json where T : class, new() { private volatile static Json _Instance; public static Json GetInstance => _Instance ?? (_Instance = new Json()); private Json() { } static string path { get { Directory.CreateDirectory(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "AccessFile")); return $"{AppDomain.CurrentDomain.BaseDirectory}AccessFile\\{typeof(T).Name}.json"; } } public T Base { get; set; } = new T(); /// /// 保存数据 /// public void Save() { string outjson = JsonConvert.SerializeObject(Base); File.WriteAllText(path, outjson); } /// /// 获取保存的数据 /// public void Read() { if (File.Exists(path)) { string JsonString = File.ReadAllText(path); var result = JsonConvert.DeserializeObject(JsonString); if (result != null) { Base = result; } } } } }