ConfigurationManager中不保存设置 [英] ConfigurationManager doesn't save settings
本文介绍了ConfigurationManager中不保存设置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
下面是code我使用的是:
Here is the code I'm using:
private void SaveConfiguration()
{
if (txtUsername.Text != "" && txtPassword.Text != "")
{
ConfigurationManager.AppSettings["Username"] = txtUsername.Text;
ConfigurationManager.AppSettings["Password"] = txtPassword.Text;
MessageBox.Show("Su configuracion guardo exitosamente.", "Exito!");
this.Close();
}
else
{
MessageBox.Show("Por favor lleno los campos.", "Error.");
}
}
现在,这些设置将持续存在,但是当我关闭应用程序和preSS F5再次运行它,值将恢复到什么键入到app.config文件。有什么建议?
Now, the settings are persisted but when I close the application and press F5 to run it again, the values are reverted to what is typed into the app.config file. Any suggestions?
推荐答案
我觉得你应该调用Save方法
I think you should call the Save method
ConfigurationManager.Save(ConfigurationSaveMode.Modified);
ConfigurationManager.RefreshSection("appSettings");
修改
要能救你必须使用由OpenExeConfiguration方法返回的配置对象。
To be able to save you have to use a configuration object returned by the OpenExeConfiguration Method
//Create the object
Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
//make changes
config.AppSettings.Settings["Username"].Value = txtUsername.Text;
config.AppSettings.Settings["Password"].Value = txtPassword.Text;
//save to apply changes
config.Save(ConfigurationSaveMode.Modified);
ConfigurationManager.RefreshSection("appSettings");
更多参考这里<一个href=\"http://msdn.microsoft.com/en-us/library/system.configuration.configurationmanager.aspx\">ConfigurationManager类
这篇关于ConfigurationManager中不保存设置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文