简体   繁体   English

在运行时从配置文件写入和读取更新的appSettings

[英]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.

相关问题 从 appsettings.json 配置文件中读取 - Read from appsettings.json config file 从web.config中的appsettings中读取文件属性 - Read file attribute from appsettings from web.config 使用WebConfigurationManager从Web.config文件中读取appSettings部分 - Read appSettings section from Web.config file using WebConfigurationManager 如何从多个位置标签中有多个appSettings标签的配置文件中读取appSettings? - How to read appSettings from a config file where there are multiple appSettings tag inside multiple location tag? 如何在 DI 设置期间自动验证 appSettings.json 文件中的配置值? - How to validate config values from appSettings.json file automatically during DI setup? 在运行时更新配置文件 - Update config file during runtime AppSettings 从 .config 文件获取值 - AppSettings get value from .config file 努力在 ASP .NET 中使用 ConfigurationManager.AppSettings[“key”] 从 web.config 文件中读取值 - Struggling to read value from web.config file using ConfigurationManager.AppSettings[“key”] in ASP .NET 如何在运行时正确创建并读取存储在先前 session 期间保存的配置文件中的属性? - How do I properly create during runtime and read the properties that are stored in the config file that saved during a previous session? ConfigurationManager.AppSettings[Key] 是否每次都从 web.config 文件中读取? - Does ConfigurationManager.AppSettings[Key] read from the web.config file each time?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM