[英]Write and read updated appSettings from config file during runtime
Problem: 问题:
I have a program where i write Keys+Values in my appconfig during runtime, but when i want to read them i get the old values and the only way to get the new values is by restarting the application. 我有一个程序,我在运行时在我的appconfig中编写Keys + Values,但是当我想读取它们时,我得到了旧值,而获得新值的唯一方法是重新启动应用程序。
As soon as i write the key+value programmically the config file is updated so that is not the problem, but i cannot figure out why i wont get the new values during the same runtime. 一旦我以编程方式编写键+值,配置文件就会更新,因此这不是问题,但是我无法弄清楚为什么我在同一运行时无法获得新值。
I write like this: (tried with and without RefreshSection(key) - made no difference) 我这样写:( 尝试使用和不使用RefreshSection(key)-没什么区别)
public static void AddValue(string key, string value)
{
Configuration config = ConfigurationManager.OpenExeConfiguration(System.Reflection.Assembly.GetEntryAssembly().Location);
config.AppSettings.Settings.Add(key, value);
config.Save(ConfigurationSaveMode.Full);
ConfigurationManager.RefreshSection(key);
}
And i read it like this: 我这样读:
string[] ItemsArray = ConfigurationManager.AppSettings["Items"].Split(',');
Question: 题:
How can i read the new keys (in runtime) which i added during the same runtime? 如何读取在同一运行时间期间添加的新密钥(在运行时间中)?
You should try 你应该试试
ConfigurationManager.RefreshSection("appSettings");
Found an old post here Reloading configuration without restarting application using ConfigurationManager.RefreshSection 在这里找到了一个旧帖子, 无需使用ConfigurationManager.RefreshSection即可重新加载配置而不重新启动应用程序
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.