10 April 2006 in .Net, Code, Intergen | Comments enabled

This is more for my own reminder but somebody else might find it useful. This code allows you to update the value of a config key in your .net 2.0 application. It also updates the section so subsequent reads from the .config will reflect that change. Really useful when you just need to store a single peice of information.

In this example I’m setting a last run date.

// Open the app.config
System.Configuration.Configuration config = ConfigurationManager
.OpenExeConfiguration(ConfigurationUserLevel.None);

// Update the last run time 
config.AppSettings.Settings["LastRunDate"].Value
= lastRunTime.ToString();

// Save the configuration file
config.Save(ConfigurationSaveMode.Modified);

// Force a reload of a changed section.
ConfigurationManager.RefreshSection("appSettings");

- JD

Average Rating: 4.7 out of 5 based on 295 user reviews.


2 comments. Add your own comment.

Dan says 22 April 2006 @ 09:06

I knew there was an easy way to do this! I was digging around on msdn2 for an hour before I found this. Thanks for posting it even if it was just a reminder for yourself :)

traskjd says 22 April 2006 @ 20:10

Great to know that sometimes my blog helps people out :)

– JD

Leave a Comment

Name (required)

E-mail (required - not published)

Website

Your comment: