using Newtonsoft.Json; namespace SHH.MjpegPlayer; /// /// Json 配置文件 /// public class JsonConfig { #region Load /// /// 加载配置 /// /// /// /// public static T? Load(string path) { try { var newPath = $"{Environment.CurrentDirectory}\\{path}"; path = newPath.Replace("Res\\Plugins\\", ""); var sr = new StreamReader(path); var data = sr.ReadToEnd(); sr.Close(); sr = null; data = data.Replace(@"""$schema"": ""https://gitee.com/dotnetchina/Furion/raw/v4/schemas/v4/furion-schema.json"",", ""); var obj = JsonConvert.DeserializeObject(data); //Logs.LogInformation(EIdFiles.LoadSucceed, // $"配置{EIdFiles.LoadSucceed.GetDescription()}, Path:{path} 类型:{typeof(T).FullName}."); return obj; } catch (Exception ex) { //Logs.LogWarning(EIdFiles.LoadFailed, // $"配置{EIdFiles.LoadSucceed.GetDescription()}, Path:{path} 类型:{typeof(T).FullName}.", ex.Message, ex.StackTrace); return default(T); } } #endregion #region Save /// /// 保存配置 /// /// /// /// /// public static bool Save(object obj, string path, string caption) { try { var newPath = Path.GetFullPath(path); if (File.Exists(newPath)) File.Delete(newPath); var loc = newPath.LastIndexOf("\\"); if (loc > 0) { var newDir = newPath.Substring(0, loc); Directory.CreateDirectory(newDir); } var msg = JsonConvert.SerializeObject(obj, Formatting.Indented); msg = msg.Insert(1, "\"$schema\": \"https://gitee.com/dotnetchina/Furion/raw/v4/schemas/v4/furion-schema.json\",\r\n"); var sw = new StreamWriter(newPath); sw.Write(msg); sw.Flush(); sw.Close(); sw = null; //Logs.LogInformation(EIdFiles.SaveSucceed, // $"配置{EIdFiles.SaveSucceed.GetDescription()}, Path:{path}\r\n\t\t\tCaption:{caption} 类型:{obj.GetType().FullName}."); return true; } catch (Exception ex) { //Logs.LogInformation(EIdFiles.SaveFailed, // $"配置{EIdFiles.SaveFailed.GetDescription()}, Path:{path}\r\n\t\t\tCaption:{caption} 类型:{obj.GetType().FullName}.", ex.Message, ex.StackTrace); return false; } } #endregion }