【问题标题】:Can't modify app.config file with no error无法修改 app.config 文件且没有错误
【发布时间】:2014-11-19 21:28:03
【问题描述】:

我正在努力尝试修改我的应用程序的 app.config 文件的 appSettings 部分。但是我没有收到任何错误/异常,我可以毫无问题地读取值。 这是我尝试设置值的方式:

ConfigurationManager.AppSettings.Set("DaysLeft", Days.Text.ToString());
//    
System.Configuration.Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
config.AppSettings.Settings["DaysLeft"].Value = Days.Text.ToString();
//when I debug the app, I notice that the value is changed in the previous line

app.config 文件未被修改。

【问题讨论】:

标签: c# app-config appsettings


【解决方案1】:
System.Configuration.Configuration config = 
         ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
config.AppSettings.Settings["DaysLeft"].Value = Days.Text.ToString();
config.Save(); 

【讨论】:

    【解决方案2】:

    您需要保存更改

    config.Save(ConfigurationSaveMode.Modified);
    

    另外,如果你想重新加载 app.config

    ConfigurationManager.RefreshSection("appSettings");
    

    【讨论】:

      猜你喜欢
      • 2012-02-19
      • 1970-01-01
      • 1970-01-01
      • 2013-03-21
      • 2023-03-11
      • 1970-01-01
      • 1970-01-01
      • 2012-11-13
      • 1970-01-01
      相关资源
      最近更新 更多