This is my App.Config
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key="lang" value="English"/>
</appSettings>
</configuration>
With this code I make the change
lang = "Russian";
private void Main_FormClosing(object sender, FormClosingEventArgs e)
{
System.Configuration.ConfigurationManager.AppSettings.Set("lang", lang);
}
But it not change. What I'm doing wrong?
You cannot use AppSettings static object for this. Try this
when use "ConfigurationUserLevel.None" your code is right run when you click in nameyourapp.exe in debug folder. .
but when your do developing app on visual stdio not right run!! because "vshost.exe" is run.
following parameter solve this problem : "Application.ExecutablePath"
try this : (Tested in VS 2012 Express For Desktop)
my english not good , i am sorry.
In addition to the answer by fenix2222 (which worked for me) I had to modify the last line to:
Without this, the new value was still being written to the config file but the old value was retrieved when debugging.
Thanks Jahmic for the answer. Worked properly for me.
another useful code snippet that read the values and return a string:
AppSettings.Set
does not persist the changes to your configuration file. It just changes it in memory. If you put a breakpoint onSystem.Configuration.ConfigurationManager.AppSettings.Set("lang", lang);
, and add a watch forSystem.Configuration.ConfigurationManager.AppSettings[0]
you will see it change from "English" to "Russian" when that line of code runs.The following code (used in a console application) will persist the change.
From this post: http://vbcity.com/forums/t/152772.aspx