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

问题描述 投票:0回答:2

我正在使用C#Windows Form Application。我正在为我的申请使用2种表格。我有一个app.config文件来保存设置运行时间。

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

我让我的应用程序开始运行。运行时,我通过Form1更新了app.config中的设置。之后,我单击按钮以打开Form2并进行了一些修改,然后尝试将设置保存到相同的app.config文件中。但是它抛出了一个异常,说

配置文件已被另一个程序更改。

我不知道我哪里出了问题。请帮助我使它工作。预先感谢。

编辑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();
    }

这是我的表格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();
    }
c# winforms app-config
2个回答
3
投票

在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();
  }
}

0
投票

Form 2无法覆盖app.config,因为它仍被Form 1使用(反之亦然)。因此,保存后需要使Configuration变量无效。

由于配置不是可分配的ID,因此using语句不起作用。

表格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;
    }
© www.soinside.com 2019 - 2024. All rights reserved.