How to store key-value pairs in application settings at runtime?

How are dynamic key pairs and object values โ€‹โ€‹stored in app.config using api application parameters at runtime?

I am trying to turn my head around and I cannot find any meaningful example or documentation.

It seems that .Net vocabulary classes cannot be serialized in XML to be stored in app.config

Is this the only way to do this using custom semantized XML classes, or are there other ways?

+4
source share
5 answers

After several search networks, I found a very good (albeit very long) article that describes the .Net configuration model:

Unpacking the secrets of the .NET 2.0 configuration

I also found something very useful:

Configuration Section Constructor

A visual studio has been added for the visual design of configuration sections and the creation of XSD files. I hope someone finds this helpful.

+2
source

You should be able to do something like this:

// Open App.Config of executable System.Configuration.Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None); // Add an Application Setting with a name Key and a Value stored in variable called Value config.AppSettings.Settings.Add("Key", Value ); // Save the changes in App.config file. config.Save(ConfigurationSaveMode.Modified); 
+2
source

It was always thought that it was considered wrong practice to modify app.config at run time. App.config should be used to configure and configure the program; not for dynamic themes that the application really modifies.

Suggest creating a separate "KeyValue.xml" file for dynamic objects.

+1
source

Depending on what you need, you may try System.Collections.ObjectModel.KeyedCollection <TKey, TValue>. This only works if you can get the key for the element from the element itself, but in this case it is suitable for this purpose.

0
source

Is this a common dictionary? If yes:

XML-Serializable Generic Dictionary

0
source

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


All Articles