0

I have a configuration file like this:

<configSections>

    <section
        name="environmentVariables"
        type="System.Configuration.NameValueFileSectionHandler, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=234effdfsdfdf" />

</configSections>

<environmentVariables>
    <add key="xxx" value="yyyy" />
    <add key="zz" value="ddd" />
</environmentVariables>

I need to change the values of key "xxx" and "zz" during runtime from C#.

I have accessed the file using the code below:

string configPath = HttpContext.Server.MapPath("~/files/temp.config");
ExeConfigurationFileMap configFileMap = new ExeConfigurationFileMap();
configFileMap.ExeConfigFilename = configPath;
System.Configuration.Configuration config = 
    ConfigurationManager.OpenMappedExeConfiguration(configFileMap, ConfigurationUserLevel.None);
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Neeno Xavier
  • 551
  • 1
  • 4
  • 3

1 Answers1

0

Here is the example:

Configuration config = ConfigurationManager.OpenExeConfiguration(Application.ExecutablePath);
config.AppSettings.Settings.Add("YourKey", "YourValue");
config.Save(ConfigurationSaveMode.Minimal);

Source: Write values in app.config file

Community
  • 1
  • 1
Marek
  • 3,555
  • 17
  • 74
  • 123