/// <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"); } }
修改Config文件
猜你喜欢
转载自www.cnblogs.com/miss-bug/p/9159618.html
今日推荐
周排行