ConfigurationManager doesn't save settings

心不动则不痛 提交于 2019-11-26 06:46:47

问题


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.\");
    }
}

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?


回答1:


I think you should call the Save method

ConfigurationManager.Save(ConfigurationSaveMode.Modified);
ConfigurationManager.RefreshSection("appSettings");

EDIT

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");

More references here ConfigurationManager Class




回答2:


When you run your application with F5,

  • your code is compiled,
  • the executable is copied to the bin or bin\Debug subdirectory of your source code directory,
  • your app.config is copied as yourexecutable.exe.config into that directory, and
  • your executable is started in that directory.

Thus, your application uses the yourexecutable.exe.config in the bin or bin\Debug directory, and it is there that ConfigurationManager saves the changes, not in your source code directory. This won't be an issue after deploying your application, because then, changes will go to yourexecutable.exe.config in the deployment directory, which is what you want.




回答3:


Further to Appetere's comment on the second answer:

Also note that if you're debugging (and haven't disabled the vshost process), then when your application stops, yourexecutable.vshost.exe.config will be replaced with yourexecutable.exe.config again.

So once again, you may not see any changes you made afterwards! (If you stop at a breakpoint whilst debugging and look in the file after making your modification and calling refresh section, you'll see your changes).

This is very confusing if you are debugging a program which looks for a setting and, if not present, writes it. Even if you're forewarned against expecting the the setting to be there the second time you run the program, one might expect it to be there AFTER the first run of the program and BEFORE the second run ... alas!

It's nothing to worry about since it all just works when the application is deployed or started directly from bin as others have already stated...

But it's possible to fall into a 'trap' though if you're debugging your program and decide to use Application Settings for the first time, and to avoid hand-writing the XML you decide you'll start from code and get the program to write a setting... to get all that stuff, then maybe add several more.



来源:https://stackoverflow.com/questions/4216809/configurationmanager-doesnt-save-settings

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!