【问题标题】:app.config not updating when I use configmanager当我使用 configmanager 时 app.config 没有更新
【发布时间】:2016-07-21 15:19:32
【问题描述】:

我有这些代码:

string theme = ConfigurationManager.AppSettings["Theme"];


private void ChangeTheme(string Name)
    {
        if(Name=="Light")
        {
            Form1.ActiveForm.BackColor = System.Drawing.Color.White;
            Form.ActiveForm.ForeColor = System.Drawing.Color.Black;
        }
        if (Name == "Dark")
        {
            Form1.ActiveForm.BackColor = System.Drawing.Color.Black;
            Form.ActiveForm.ForeColor = System.Drawing.Color.DarkOrange;
        }
        Configuration cfg = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
        cfg.AppSettings.Settings["Theme"].Value = Name;
        cfg.Save(ConfigurationSaveMode.Modified);
        ConfigurationManager.RefreshSection("appSettings");
    }

我的 app.config:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <appSettings>
    <add key="Volume" value="7"/>
    <add key="Keyval" value="X"/>
    <add key="Theme" value="Light"/>
  </appSettings>
</configuration>

基本上,当我按下单选按钮时,它会更改主题并将字符串发送到 changetheme(),但它不会在 app.config 中更新。

【问题讨论】:

标签: c# winforms app-config configurationmanager


【解决方案1】:

你是在调试中运行它吗?

您可能正在查看错误的文件。在调试 Visual Studio 时使用 bin\Debug 中的配置文件,因此它不会从解决方案中更新 App.config。

【讨论】:

    【解决方案2】:

    我不会使用 == 进行比较,而是使用字符串等于方法:

    Name.Equals("Light")
    

    虽然我敢打赌这不是问题,但这是一种很好的做法,也许你很幸运,这就是问题所在。

    【讨论】:

      猜你喜欢
      • 2013-10-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-03-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-03-31
      相关资源
      最近更新 更多