C# 写App.config配置文件的方法

private static void AccessAppSettings()
{
//获取Configuration对象
Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
//根据Key读取<add>元素的Value
string name = config.AppSettings.Settings["count"].Value;
//写入<add>元素的Value
config.AppSettings.Settings["count"].Value = "fx163";
//增加<add>元素
config.AppSettings.Settings.Add("url", "http://www.fx163.net");
//删除<add>元素
config.AppSettings.Settings.Remove("name");
//一定要记得保存,写不带参数的config.Save()也可以
config.Save(ConfigurationSaveMode.Modified);
//刷新,否则程序读取的还是之前的值(可能已装入内存)
ConfigurationManager.RefreshSection("appSettings");
}

转载:https://www.cnblogs.com/feiyuhuo/p/5243967.html

猜你喜欢

转载自www.cnblogs.com/uftwkb24/p/9914465.html