/// <summary>
    /// Config文件操作
    /// </summary>
    public class Config
    {
        /// <summary>
        /// 摘要:获取App.Config节点的值
        /// </summary>
        public static string GetValue(string key)
        {
            ExeConfigurationFileMap map = new ExeConfigurationFileMap();
            map.ExeConfigFilename = AppDomain.CurrentDomain.BaseDirectory + "/App.config";
            Configuration a = ConfigurationManager.OpenMappedExeConfiguration(map, ConfigurationUserLevel.None);
            //如果配置文件中不包括appSettings节,则抛出异常.
            if (a.Sections["appSettings"] == null)
            {
                throw new Exception("该配置文件中不存在appSettings节点");
            }
            else
            {
                return a.AppSettings.Settings[key]?.Value.ToString();
            }
        }
        /// <summary>
        /// 根据Key修改Value
        /// </summary>
        /// <param name="key">要修改的Key</param>
        /// <param name="value">要修改为的值</param>
        public static void SetValue(string key, string value)
        {
            Configuration cfa = ConfigurationManager.OpenExeConfiguration(AppDomain.CurrentDomain.BaseDirectory + "/App.config");
            //修改
            cfa.AppSettings.Settings[key].Value = value;
            cfa.Save();
            ConfigurationManager.RefreshSection("appSettings");
        }
    }

 

posted on 2018-06-09 15:18  Miss_Bug  阅读(202)  评论(0编辑  收藏  举报