using Newtonsoft.Json; using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; namespace KMBIM.Revit.Tools.Utils { public class SettingManager { private static string Init() { return Path.Combine(Path.GetDirectoryName(typeof(SettingManager).Assembly.Location), "Setting.json"); } private static bool Init(string configFileName = "") { if (configFileName == "") { configFileName = Init(); } if (!File.Exists(configFileName)) { return false; } SettingHelper.Load(configFileName); return true; } public static T GetSetting(string indexKey, string settingFile = "") where T : class, new() { try { if (!Init(settingFile)) { return new T(); } string value = SettingHelper.Configuration !=null ? SettingHelper.Configuration[indexKey] : null; if (string.IsNullOrEmpty(value)) { return new T(); } return JsonConvert.DeserializeObject(value); } catch (Exception) { return new T(); } } public static bool SetSetting(string indexKey, T val, string settingFile = "") { try { if (val == null) { return false; } if (!Init(settingFile)) { return false; } SettingHelper.Configuration[indexKey] = JsonConvert.SerializeObject(val); SettingHelper.Save(); return true; } catch (Exception) { return false; } } } }