Muhammad Faisal
Muhammad Faisal

Reputation: 135

App.Config not updating

I want to update app. config file in windows form c# . here is a code for updating app. config

 // updating
 System.Configuration.Configuration config =  ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
 config.AppSettings.Settings.Remove("name");
 config.AppSettings.Settings.Add("name",txtName.Text);
 config.Save(ConfigurationSaveMode.Modified);
 ConfigurationManager.RefreshSection("appSettings");

 // show updating result on labels ( and this work fine )
 string value = ConfigurationManager.AppSettings["name"];
 lblName.Text = value;

this update work fine when I am running an application but when I restart application all config is reset to default

Upvotes: 2

Views: 2089

Answers (1)

user3624833
user3624833

Reputation:

Instead of using the Configuration class, open the file as a regular Xml file and make your changes. Note that when you are doing this, when you save the file back, any Comments in your .config file will be removed by the Xml class. To prevent this, you must read ALL types of Xml nodes from the original configuration file and write them all back.

Upvotes: 1

Related Questions