Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I reload/refresh app.config?

I want to read the app.config value, show it in a message box, change the value using an external text editor, and finally show the updated value.

I tried using the following code:

private void button2_Click(object sender, EventArgs e)
{
    Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
    ConfigurationManager.RefreshSection("appSettings");
    ConfigurationManager.RefreshSection(config.AppSettings.SectionInformation.Name);
    MessageBox.Show(ConfigurationManager.AppSettings["TheValue"]);
}

But it doesn't work. It shows the old value (before changing in external text editor). Any suggestions?

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
  <add key="TheValue" value="abc"/>
</appSettings>
</configuration>
like image 782
Alex Jones Avatar asked Apr 07 '13 10:04

Alex Jones


People also ask

How do I install app config?

To add an application configuration file to a C# projectIn the middle pane, select the Application Configuration File template. Select the Add button. A file named App. config is added to your project.

Where are app config files stored?

The file is stored inside this path "bin/debug/app. config", if you make changes while debugging, those changes should appear there. Just remember that this file is overwritten with the "app. config" from the project root each time you run the application on Visual Studio IDE.

Can I delete app config?

If the app has a configuration profile, delete it. Go to Settings > General > Profiles or Profiles & Device Management,* then tap the app's configuration profile. Then tap Delete Profile.

What's app config?

At its simplest, the app. config is an XML file with many predefined configuration sections available and support for custom configuration sections. A "configuration section" is a snippet of XML with a schema meant to store some type of information.


2 Answers

It may help you

try to save configuration like this

System.Configuration.Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
config.AppSettings.Settings["KeyName"].Value = "NewValue";
config.AppSettings.SectionInformation.ForceSave = true;
config.Save(ConfigurationSaveMode.Modified);

and then fetch it like this

ConfigurationManager.RefreshSection("appSettings");
System.Configuration.Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
like image 164
Sachin Avatar answered Oct 03 '22 00:10

Sachin


You can try using the following code:

Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
KeyValueConfigurationCollection settings = config.AppSettings.Settings;            
// update SaveBeforeExit
settings["TheValue"].Value = "WXYZ";
config.Save(ConfigurationSaveMode.Modified);

MessageBox.Show(ConfigurationManager.AppSettings["TheValue"]);
like image 44
Gencebay Avatar answered Oct 02 '22 22:10

Gencebay