簡體   English   中英

在運行時從配置文件寫入和讀取更新的appSettings

[英]Write and read updated appSettings from config file during runtime

問題:

我有一個程序,我在運行時在我的appconfig中編寫Keys + Values,但是當我想讀取它們時,我得到了舊值,而獲得新值的唯一方法是重新啟動應用程序。

一旦我以編程方式編寫鍵+值,配置文件就會更新,因此這不是問題,但是我無法弄清楚為什么我在同一運行時無法獲得新值。

我這樣寫:( 嘗試使用和不使用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);
}

我這樣讀:

string[] ItemsArray = ConfigurationManager.AppSettings["Items"].Split(',');

題:

如何讀取在同一運行時間期間添加的新密鑰(在運行時間中)?

你應該試試

ConfigurationManager.RefreshSection("appSettings");

在這里找到了一個舊帖子, 無需使用ConfigurationManager.RefreshSection即可重新加載配置而不重新啟動應用程序

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM