Link to home
Start Free TrialLog in
Avatar of Paracom_Inc
Paracom_Inc

asked on

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?
Avatar of surajguptha
surajguptha
Flag of United States of America image

That happens because you run it everytime from your solution. try executing the exe, it would work the way you expect it to
ASKER CERTIFIED SOLUTION
Avatar of surajguptha
surajguptha
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Avatar of Bob Learned
Bob Learned
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Paracom_Inc
Paracom_Inc

ASKER

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");
      }
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial