76 lines
2.0 KiB
C#
76 lines
2.0 KiB
C#
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<T>(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<T>(value);
|
|
}
|
|
catch (Exception)
|
|
{
|
|
return new T();
|
|
}
|
|
}
|
|
|
|
public static bool SetSetting<T>(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;
|
|
}
|
|
}
|
|
}
|
|
}
|