Avatar of purplesoup
purplesoup
Flag for United Kingdom of Great Britain and Northern Ireland asked on

How to write to the app.config file?

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" ?>
<configuration>
  <appSettings>
    <add key="LastSearchPath" value=""/>
  </appSettings>

Here is my current code:

            System.Configuration.Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);            
            config.AppSettings.Settings["LastSearchPath"].Value = txtPath.Text;
            config.Save(ConfigurationSaveMode.Modified);
            ConfigurationManager.RefreshSection("appSettings");

C#.NET ProgrammingVisual Basic.NET

Avatar of undefined
Last Comment
graye

8/22/2022 - Mon
SOLUTION
Om Prakash

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
ASKER CERTIFIED SOLUTION
w00te

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Mohit Vijay

Why not you try to save settings in a Database table, you can create a tblAppSettings (example) table in Database and once your application loaded, you can retrive all settings from table and store all config values as xml on any physical path, and can retrieve information whenever required.
SOLUTION
graye

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy