天津投入产出系统后端
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

137 lines
4.0 KiB

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
namespace QMAPP.WinForm.Common
{
/// <summary>
/// 工控机本地设置
/// </summary>
public class LocalSetting
{
private const string filename = "LocalSetting.cfg";
private static Dictionary<string,string> _settinglist;
private static LocalSetting _settings = null;
/// <summary>
/// 本地配置
/// </summary>
public static LocalSetting Settings
{
get {
if (_settings == null)
{
_settings = new LocalSetting();
}
return _settings;
}
}
private LocalSetting()
{
Load();
}
/// <summary>
/// 加载配置
/// </summary>
private static void Load()
{
string filepath = ConfigurationManager.AppSettings["SaveFilePath"];
filepath = string.Format("{0}\\{1}", filepath.TrimEnd('\\'), filename);
if (System.IO.File.Exists(filepath))
{
string settingstring = System.IO.File.ReadAllText(filepath);
_settinglist = Newtonsoft.Json.JsonConvert.DeserializeObject<Dictionary<string, string>>(settingstring);
}
if (_settinglist == null)
{
_settinglist = new Dictionary<string, string>();
}
}
/// <summary>
/// 保存配置
/// </summary>
private static void Save()
{
if (_settinglist != null)
{
string filepath = ConfigurationManager.AppSettings["SaveFilePath"];
filepath = string.Format("{0}\\{1}", filepath.TrimEnd('\\'), filename);
string settingstring = Newtonsoft.Json.JsonConvert.SerializeObject(_settinglist);
System.IO.File.WriteAllText(filepath, settingstring);
}
}
/// <summary>
/// 获取或设置本地配置值
/// </summary>
/// <param name="key">key</param>
/// <returns></returns>
public string this[string key]
{
get
{
if (_settinglist != null && _settinglist.ContainsKey(key))
{
return _settinglist[key];
}
return null;
}
set
{
if (_settinglist != null)
{
if (!_settinglist.ContainsKey(key))
{
_settinglist.Add(key, value);
}
else
{
_settinglist[key] = value;}
Save();
}
}
}
/// <summary>
/// 获取或设置本地配置值,如果不存在配置则返回默认值并保存
/// </summary>
/// <param name="key">key</param>
/// <param name="defvalue">默认值</param>
/// <returns></returns>
public string this[string key,string defvalue]
{
get
{
if (_settinglist != null)
{
if (!_settinglist.ContainsKey(key))
{
_settinglist.Add(key, defvalue);
Save();
}
return _settinglist[key];
}
return null;
}
set
{
if (_settinglist != null)
{
if (!_settinglist.ContainsKey(key))
{
_settinglist.Add(key, value);
}
else
{
_settinglist[key] = value;
}
Save();
}
}
}
}
}