using DownKyi.Core.Settings.Models;
using Newtonsoft.Json;
using System;
using System.IO;
namespace DownKyi.Core.Settings
{
public partial class SettingsManager
{
private static SettingsManager instance;
///
/// 获取SettingsManager实例
///
///
public static SettingsManager GetInstance()
{
if (instance == null)
{
instance = new SettingsManager();
}
return instance;
}
///
/// 隐藏Settings()方法,必须使用单例模式
///
private SettingsManager()
{
appSettings = GetSettings();
}
// 内存中保存一份配置
private AppSettings appSettings;
// 设置的配置文件
private readonly string settingsName = Storage.StorageManager.GetSettings();
///
/// 获取AppSettingsModel
///
///
private AppSettings GetSettings()
{
try
{
StreamReader streamReader = File.OpenText(settingsName);
string jsonWordTemplate = streamReader.ReadToEnd();
streamReader.Close();
return JsonConvert.DeserializeObject(jsonWordTemplate);
}
catch (Exception e)
{
Logging.LogManager.Error(e);
return new AppSettings();
}
}
///
/// 设置AppSettingsModel
///
///
private bool SetSettings()
{
string json = JsonConvert.SerializeObject(appSettings, Formatting.Indented);
try
{
File.WriteAllText(settingsName, json);
return true;
}
catch (Exception e)
{
Logging.LogManager.Error(e);
return false;
}
}
}
}