【发布时间】:2015-03-18 19:17:45
【问题描述】:
我正在使用 C# Windows 窗体应用程序。我正在为我的申请使用 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();
}
【问题讨论】:
-
如果只是将数据保存到文件中,我建议您使用
XML文件。显示您的代码。 -
谢谢@RohitGupta。但我没有时间。那么我有什么方法可以在 app.config 本身中做到这一点吗?
-
您在更新后保存配置对吗?
config.save() -
修改form1中的值并做form2代码后应该使用
Properties.Settings.Default.Reload(); -
是的@RohitGupta 我有一个按钮来保存修改后的数据。我正在使用 config.save() 函数来保存它
标签: c# winforms app-config