?
Solved

Writing changes to app.config appSettings section at runtime

Posted on 2007-10-10
7
Medium Priority
?
5,659 Views
Last Modified: 2013-11-07
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?
0
Comment
Question by:Paracom_Inc
7 Comments
 
LVL 21

Expert Comment

by:surajguptha
ID: 20049871
That happens because you run it everytime from your solution. try executing the exe, it would work the way you expect it to
0
 
LVL 21

Accepted Solution

by:
surajguptha earned 600 total points
ID: 20049892
It happens if you run the solution because the app.config in your solution overwrites the app.exe.config in the debug folder
0
 
LVL 96

Assisted Solution

by:Bob Learned
Bob Learned earned 300 total points
ID: 20050006
You should be using the Settings, since they are read-writable, instead of the app.config file (which is read-only).

Bob
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 70

Assisted Solution

by:Éric Moreau
Éric Moreau earned 300 total points
ID: 20050607
0
 
LVL 8

Assisted Solution

by:slado2
slado2 earned 300 total points
ID: 20051131
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
 

Author Comment

by:Paracom_Inc
ID: 20051608
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
 
LVL 21

Assisted Solution

by:surajguptha
surajguptha earned 600 total points
ID: 20051626
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

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Integration Management Part 2
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
Suggested Courses
Course of the Month16 days, 7 hours left to enroll

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question