Guy
Guy

Reputation: 335

Updating a value in app.config at runtime

I've got two projects under the same solution. I use one project to update the app.config file of the second project. I manage to read the values I need, by using the GetSection method and the ClientSettingsSection class, but I can't find how to update those values.

Upvotes: 1

Views: 1597

Answers (2)

user596075
user596075

Reputation:

You can do something like this:

Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
config.AppSettings.Settings.Add("MyKey", "MyValue);
config.Save(ConfigurationSaveMode.Modified);

But the application configuration file is cached, so you need to call the ConfigurationManager.RefreshSection() method: http://msdn.microsoft.com/en-us/library/system.configuration.configurationmanager.refreshsection.aspx

Upvotes: 0

genesis
genesis

Reputation: 50982

ConfigurationManager.RefreshSection(sectionName);

Do you mean this?

Upvotes: 2

Related Questions