C#之app.config、exe.config和vshost.exe.config作用区别


640?wx_fmt=png

vshost.exe.config是程序运行时的配置文本 
exe.config是程序运行后会复制到vshost.exe.config 
app.config是在vshost.exe.config和exe.config没有情况起作用,从app.config复制到exe.config再复制到vshost.exe.config

写配置文件都是写到exe.config文件中了,app.config不会变化。 
app.config只在exe.config丢失的情况下在开发环境中重新加载app.config,vshost.exe.config和exe.config会自动创建内容跟app.config一样。

vshost.exe.config和app.config两个文件可不要,但exe.config文件不可少。

网络上有很多文章是讲app.config读写方法的,可是事实上这个文件程序就不能改变它。下面是动态修改配置文件的方法:


     public static void SaveAppSettings(string aKey, string aValue)
{
// 创建配置文件对象
string file = System.Windows.Forms.Application.ExecutablePath;

//此处修改为.vshost.exe.Config的内容
// System.Configuration.Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);


System.Configuration.Configuration config
= ConfigurationManager.OpenExeConfiguration(file);


if (config.AppSettings.Settings[aKey] != null)
{
// 修改
config.AppSettings.Settings[aKey].Value = aValue;
}
else
{
// 添加
AppSettingsSection ass = (AppSettingsSection)config.GetSection("appSettings");
ass.Settings.Add(aKey, aValue);
}

// 保存修改
config.Save(ConfigurationSaveMode.Modified);

// 强制重新载入配置文件的连接配置节
ConfigurationManager.RefreshSection("appSettings");
}


原文:https://www.cnblogs.com/ShaYeBlog/p/8303776.html

posted @   天使不哭  阅读(318)  评论(0编辑  收藏  举报
编辑推荐:
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· .NET10 - 预览版1新功能体验(一)
历史上的今天:
2018-08-20 SQL中Group By的用法整理
2018-08-20 SQL中Group By的用法整理
2018-08-20 SQL中Group By的用法整理
点击右上角即可分享
微信分享提示