Here is the code I\'m using:
private void SaveConfiguration()
{
if (txtUsername.Text != \"\" && txtPassword.Text != \"\")
{
Configura
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.
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
When you run your application with F5,
bin
or bin\Debug
subdirectory of your source code directory,app.config
is copied as yourexecutable.exe.config
into that directory, andThus, 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.