C# 配置文件已被C中的另一个程序更改#
我在C#Windows窗体应用程序中工作。我用两张表格申请。我有一个app.config文件来保存运行时的设置 我有一个按钮在我的表格1打开表格2。我有一些设置要保存在Form2和Form1中 我让我的应用程序运行。运行时,我通过Form1更新了app.config中的设置。之后,我点击按钮打开Form2,进行了一些修改,并试图将设置保存到同一app.config文件中。但它抛出了一个例外,说 配置文件已被其他程序更改 我想不出哪里出了错。请帮我把它弄好。 提前谢谢 编辑1: 这是我在表格1中的函数C# 配置文件已被C中的另一个程序更改#,c#,winforms,app-config,C#,Winforms,App Config,我在C#Windows窗体应用程序中工作。我用两张表格申请。我有一个app.config文件来保存运行时的设置 我有一个按钮在我的表格1打开表格2。我有一些设置要保存在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();
}
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();
}
}
表格2代码:
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();
}
}
表单2无法覆盖app.config,因为表单1仍在使用它(反之亦然)。因此,保存后需要将配置变量置零 using语句不起作用,因为配置不是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;
}
如果只是为了将数据保存到文件,我建议您使用
XML
file。显示您的代码。谢谢@RohitGupta。但是我没有时间。那么,我有什么方法在app.config本身中执行此操作吗?您在更新配置后正在保存配置,对吗config.save()
您应该使用Properties.Settings.Default.Reload()代码>修改form1中的值并执行form2 codeyes@RohitGupta后,我有一个按钮来保存修改后的数据。我正在使用config.save()函数来保存它,非常感谢@prasy的代码。现在它的工作就像一个魅力。再次感谢。配置不可识别,为什么要使用using语句?