using System; using System.Collections.Generic; using System.Linq; using System.Text; //using System.Threading.Tasks; namespace WPFSerialAssistant { /// /// 配置类 /// class Configuration { #region 私有字段 /// /// 默认的配置文件存储路径 /// private static readonly string defaultConfPath = @"Config\config.conf"; /// /// 默认配置文件的存储编码格式 /// private static readonly Encoding defaultConfEncoding = Encoding.Default; /// /// 存储配置信息的字典 /// private Dictionary configuration = null; #endregion private Dictionary ConfigInfo { get { return configuration; } } #region 方法 /// /// 构造函数 /// public Configuration() { if (configuration == null) { configuration = new Dictionary(); } } /// /// 私有构造函数 /// /// private Configuration(Dictionary confDic) { if (confDic == null) { this.configuration = new Dictionary(); } else { this.configuration = confDic; } } /// /// 添加配置键值对 /// /// 键 /// 值 public void Add(string key, object value) { configuration.Add(key, value); } /// /// 移除配置信息键值对 /// /// 键 public void Remove(string key) { configuration.Remove(key); } /// /// 清除所有的配置信息键值对 /// public void Clear() { configuration.Clear(); } /// /// 获取配置信息值 /// /// 键 /// public object Get(string key) { return configuration[key]; } /// /// 获取string类型的配置信息 /// /// 键 /// string类型的值 public string GetString(string key) { return Get(key).ToString(); } /// /// 获取int类型的配置信息 /// /// 键 /// int类型的值 public int GetInt(string key) { return int.Parse(Get(key).ToString()); } /// /// 获取bool类型的配置信息 /// /// 键 /// bool类型的值 public bool GetBool(string key) { return bool.Parse(Get(key).ToString()); } /// /// 获取double类型的配置信息 /// /// 键 /// double类型的值 public double GetDouble(string key) { return double.Parse(Get(key).ToString()); } /// /// 获取decimal类型的配置信息 /// /// 键 /// decimal类型的值 public decimal GetDecimal(string key) { return decimal.Parse(Get(key).ToString()); } #endregion #region 静态方法 /// /// 存储配置信息到默认的路径中 /// /// public static void Save(Configuration conf) { Save(conf, defaultConfPath); } /// /// 存储配置信息到自定义路径 /// /// /// 存储配置信息的路径 public static void Save(Configuration conf, string path) { // 构建配置信息的json字符串 string confStr = Newtonsoft.Json.JsonConvert.SerializeObject(conf.ConfigInfo); // 如果指定的配置路径不存在,则新建一个 if (System.IO.File.Exists(path) == false) { System.IO.Directory.CreateDirectory(System.IO.Path.GetDirectoryName(path)); } // 保存配置信息 using (System.IO.StreamWriter sw = new System.IO.StreamWriter(path, false, defaultConfEncoding)) { try { sw.Write(confStr); } catch (Exception ex) { throw ex; } } } /// /// 从默认存储路径读取配置信息 /// /// 配置信息 public static Configuration Read() { return Read(defaultConfPath); } /// /// 从自定义存储路径读取配置信息 /// /// 存储配置信息路径 /// 配置信息 public static Configuration Read(string path) { string confStr = ""; Configuration conf = null; if (System.IO.File.Exists(path) == false) { return null; } // 读取配置信息 using (System.IO.StreamReader sr = new System.IO.StreamReader(path, defaultConfEncoding)) { try { confStr = sr.ReadToEnd(); // 解析得到配置信息 Dictionary confInfo = Newtonsoft.Json.JsonConvert.DeserializeObject>(confStr); conf = new Configuration(confInfo); } catch (Exception ex) { throw ex; } } return conf; } #endregion } }