配置文件已被 C# 中的另一个程序更改

The configuration file has been changed by another program in C#

我正在使用 C# Windows 表单应用程序。我正在为我的应用程序使用 2 个表格。我有一个 app.config 文件来保存设置 运行 时间。

我的 Form1 中有一个按钮可以打开 Form2。我有一些设置要保存在 Form2 和 Form1 中。

我制作了我的应用 运行ning。在 运行ning 期间,我通过 Form1 更新了 app.config 中的设置。之后,我单击按钮打开 Form2 并进行了一些修改,并尝试将设置保存到相同的 app.config 文件中。但它抛出一个异常说

The configuration file has been changed by another program.

我想不通哪里出错了。请帮我让它工作。 提前致谢。

编辑 1: 这是我在表格 1

中的功能
private void button2_Click(object sender, EventArgs e)
    {

        config1.AppSettings.Settings.Add("no_of_cameras", null);
        config1.AppSettings.Settings["no_of_cameras"].Value = (no.Value).ToString("G");
        config1.Save(ConfigurationSaveMode.Modified);
        ConfigurationManager.RefreshSection("appSettings");

        Properties.Settings.Default.Reload();
    }

这是我的 Form 2 中的函数:

    private void radioButton1_CheckedChanged(object sender, EventArgs e)
    {
        config2.AppSettings.Settings.Add("mail_enable", null);

        if (radioButton1.Checked == true)
        {
            label1.Show();
            textBox1.Show();
            config2.AppSettings.Settings["mail_enable"].Value = "true";
            config2.Save(ConfigurationSaveMode.Modified);
            ConfigurationManager.RefreshSection("appSettings");
        }
        else
        {
            config2.AppSettings.Settings["mail_enable"].Value = "false";
            config2.Save(ConfigurationSaveMode.Modified);
            ConfigurationManager.RefreshSection("appSettings");
            textBox1.Hide();
            label1.Hide();
        }

        Properties.Settings.Default.Reload();
    }

Save()后,需要销毁配置对象并重新创建,多次保存操作才能正常工作。请看下面的代码。

表格 1 代码:

private void button2_Click(object sender, EventArgs e)
{
  using (Configuration config1 = System.Configuration.ConfigurationManager.OpenExeConfiguration(System.Configuration.ConfigurationUserLevel.None))
  {
    config1.AppSettings.Settings.Add("no_of_cameras", null);
    config1.AppSettings.Settings["no_of_cameras"].Value = (no.Value).ToString("G");
    config1.Save(ConfigurationSaveMode.Modified);
    ConfigurationManager.RefreshSection("appSettings");
    Properties.Settings.Default.Reload();
   }
}

Form2代码:

private void radioButton1_CheckedChanged(object sender, EventArgs e)
{
  using (Configuration config2 = System.Configuration.ConfigurationManager.OpenExeConfiguration(System.Configuration.ConfigurationUserLevel.None))
  {
    config2.AppSettings.Settings.Add("mail_enable", null);

    if (radioButton1.Checked == true)
    {
        label1.Show();
        textBox1.Show();
        config2.AppSettings.Settings["mail_enable"].Value = "true";
        config2.Save(ConfigurationSaveMode.Modified);
        ConfigurationManager.RefreshSection("appSettings");
    }
    else
    {
        config2.AppSettings.Settings["mail_enable"].Value = "false";
        config2.Save(ConfigurationSaveMode.Modified);
        ConfigurationManager.RefreshSection("appSettings");
        textBox1.Hide();
        label1.Hide();
    }

    Properties.Settings.Default.Reload();
  }
}

Form 2 无法覆盖 app.config,因为 Form 1 仍在使用它(反之亦然)。所以保存后需要将Configuration变量置空。

using 语句不起作用,因为 Configuration 不是 IDisposable。

表格 1:

private void button2_Click(object sender, EventArgs e)
    {
        Configuration config1 = System.Configuration.ConfigurationManager.OpenExeConfiguration(System.Configuration.ConfigurationUserLevel.None)

        config1.AppSettings.Settings.Add("no_of_cameras", null);
        config1.AppSettings.Settings["no_of_cameras"].Value = (no.Value).ToString("G");
        config1.Save(ConfigurationSaveMode.Modified);
        ConfigurationManager.RefreshSection("appSettings");

        Properties.Settings.Default.Reload();
        config1 = null;
    }

表格 2:

private void radioButton1_CheckedChanged(object sender, EventArgs e)
    {
        Configuration config2 = System.Configuration.ConfigurationManager.OpenExeConfiguration(System.Configuration.ConfigurationUserLevel.None)

        config2.AppSettings.Settings.Add("mail_enable", null);

        if (radioButton1.Checked == true)
        {
            label1.Show();
            textBox1.Show();
            config2.AppSettings.Settings["mail_enable"].Value = "true";
            config2.Save(ConfigurationSaveMode.Modified);
            ConfigurationManager.RefreshSection("appSettings");
        }
        else
        {
            config2.AppSettings.Settings["mail_enable"].Value = "false";
            config2.Save(ConfigurationSaveMode.Modified);
            ConfigurationManager.RefreshSection("appSettings");
            textBox1.Hide();
            label1.Hide();
        }

        Properties.Settings.Default.Reload();
        config2 = null;
    }

保存完你需要的东西,你需要销毁,添加: Properties.Settings.Default.Reload();