How to write to the app.config file?
Posted on 2010-09-07
I've got a windows forms application and I would like to save a string in the app.config file.
I've googled this and can't find any example code that works.
All I want to do is read a string in when I load my form, and save it after a particular action. The idea is just to persist a setting on the form so the user doesn't have to keep re-entering it each time the form opens.
If I set the value manually I find the string can be read with no problems, but trying to write it just doesn't seem to save it.
Here is my settings file
<?xml version="1.0" encoding="utf-8" ?>
<add key="LastSearchPath" value=""/>
Here is my current code:
System.Configuration.Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
config.AppSettings.Settings["LastSearchPath"].Value = txtPath.Text;