我在appconfig文件中有一个appsetting部分
<appSettings>
<add key="DayTime" value="08-20"/>
<add key="NightTime" value="20-08"/>
</appSettings>
我在应用程序运行时修改应用程序配置。在应用程序运行时,我将键DayTime
更改为11-20。
现在,如果我再次运行此代码以从配置中获取数据,则会显示先前的设置值。
private void btnDayNightSettings_ShowingEditor(object sender, ItemCancelEventArgs e)
{
string[] strDayTime = ConfigurationManager.AppSettings["DayTime"].Split('-');
}
为什么会这样?
答案 0 :(得分:5)
在运行时更新期间未反映AppSetting section
文件中app.config
的原因如下:
.Exe
文件夹中创建包含Debug/Release
个文件的必要文件;取决于构建模式。.config
文件,该文件看起来像YourApplicationName.exe.config
,它在原始app.config文件中保存相同的条目。 .Exe
始终引用此文件。app.config
,它实际上都会更新文件,但更新不会在YourApplicationName.exe.config
文件中更新,因为它尚未重新构建。因此,每次您需要重新构建应用程序以反映更改时。
答案 1 :(得分:2)
app.config被缓存,更改将反映您重新启动应用程序的时间。见is app.config file in WinForms cached by .Net framework?
答案 2 :(得分:1)
我有自己的答案。只需将appSettings
部分刷新为
ConfigurationManager.RefreshSection("appSettings");
答案 3 :(得分:0)
试试这个:
string strDayTime = ConfigurationManager.AppSettings["DayTime"];
ConfigurationManager.AppSettings.Set("DayTime", "11-20");
strDayTime = ConfigurationManager.AppSettings["DayTime"];
strDayTime变量的值在两行都有变化。