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>
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.
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.
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.
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.
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);
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"]);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With