Writing changes to app.config appSettings section at runtime

I have a windows forms application written in C# that needs to update its app.config file. I can update the settings for an instance of the application, but the file itself is not modified so the next time the application runs it gets the old settings. Is there some way to actually write to the app.config file?
Paracom_IncAsked:
Who is Participating?
 
surajgupthaCommented:
It happens if you run the solution because the app.config in your solution overwrites the app.exe.config in the debug folder
0
 
surajgupthaCommented:
That happens because you run it everytime from your solution. try executing the exe, it would work the way you expect it to
0
 
Bob LearnedCommented:
You should be using the Settings, since they are read-writable, instead of the app.config file (which is read-only).

Bob
0
Cloud Class® Course: Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

 
Éric MoreauSenior .Net ConsultantCommented:
0
 
slado2Commented:
If you use Settings (new in .NET 2.0) then you must realize that: Application settings are read-only and are stored in your.exe.config. User settings are read-write and are stored in user's profile.

If you don't want to use settings then you must open the .config file as a xml, modify it, save it and let configurationmanager update configuration. There's no other way. I was at a seminar with Microsoft yesterday where we discussed this.
0
 
Paracom_IncAuthor Commented:
The code that I am using is shown below. It does work properly however I run it. I had forgotten that the app.config file that would be modified was the vshost.exe.config file when running inside VS. I was checking the wrong file when looking to see if the change has been made. Even though it works, please let me know if I am doing something that I should not be doing in this code. It allows me to edit the appSettings section of the exe.config file.

      public void UpdateAppSettings(
         string settingName,
         string settingValue)
      {
         Configuration config = ConfigurationManager.OpenExeConfiguration(
            ConfigurationUserLevel.None);
         AppSettingsSection appSettings = config.AppSettings;
         KeyValueConfigurationElement setting =
            appSettings.Settings[settingName];
         setting.Value = settingValue;
         config.Save(ConfigurationSaveMode.Modified);
         ConfigurationManager.RefreshSection("appSettings");
      }
0
 
surajgupthaCommented:
Yes thats right. Like i mentionned
It happens if you run the solution because the app.config in your solution overwrites the app.exe.config in the debug folder

That happens because you run it everytime from your solution. try executing the exe, it would work the way you expect it to work
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.