using System; using System.Collections.Generic; using System.Configuration; using System.Linq; using System.Text; using System.Threading.Tasks; namespace SLH.SSDMS.Common { public class ConfigHelper { /// /// 获取配置 /// /// 节点配置 /// public static string GetConfig(string sections) { try { return ConfigurationManager.AppSettings[sections].ToString(); } catch (Exception) { } return ""; } /// /// 更新Config文件,若Key已存在,替换当前Val值;不存在则添加新的键值对 /// /// /// public static void UpdateAppConfig(string key, string value) { //增加的内容写在appSettings段下 System.Configuration.Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None); if (config.AppSettings.Settings[key] == null) { config.AppSettings.Settings.Add(key, value); } else { config.AppSettings.Settings[key].Value = value; } config.Save(ConfigurationSaveMode.Modified); ConfigurationManager.RefreshSection("appSettings");//重新加载新的配置文件 } } }