How do you prog...">

How to update the value of an existing configuration key in .NET?

Take an example ...

<add key="IDs" value="001;789;567"/>

How do you programmatically update (add) the value of only an existing key in App.config?

<add key="IDs" value="001;789;567;444"/>

There are currently the following in my code to add new keys, but I don't know how to update the keys.

System.Configuration.Configuration config = System.Configuration.ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);

config.AppSettings.Settings.Add(appKey, appKeyValue);

// Save the changes in App.config file.
config.Save(ConfigurationSaveMode.Modified);
+4
source share
1 answer

You can access it using the "Settings" key or indexer.

Configuration config = ConfigurationManager.OpenExeConfiguration("YourConfigFilePath");
config.AppSettings.Settings["IDS"].Value = "001;789;567;444";  // Update the value (You could also use your appKey variable rather than the string literal.

config.Save();

As an additional note, it may be easier to import a namespace System.Configurationrather than using an alias every time System.Configuration.

+3
source

Source: https://habr.com/ru/post/1608868/


All Articles