views:

2400

answers:

5

How can dynamic Key-value pairs of objects be stored in app.config in using the application settings api, at runtime?

I've been trying to get my head around, and I can't find any meaningful example or documentation.

I seems that .Net dictionary classes can't be serialized in XML to store them in app.config

Is the only way to do it by using custom XML serialized classes, or are there any other ways?

[Edit] useful things I found so far:

In dept article about .Net 2.0 configuration on Code Project

Unraveling the Mysteries of .NET 2.0 Configuration

Visual studion Addin:

Configuration Section Designer

A: 

Depending on what exactly you need, you may try System.Collections.ObjectModel.KeyedCollection<TKey, TValue>. This only works if you can derive the key for an item from the item itself, but in that case, it's fine for this purpose.

OregonGhost
A: 

Is it a generic dictionary? If so:

XML Serializable Generic Dictionary

steve_c
scalvert, I want to store any xml serializable object in the dictionary not just items of the same type. Basicaly the dictionary should be non generic like a HashTable.
Pop Catalin
+1  A: 

You ought to 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);
Nick
A: 

Have always thought it was considered bad practice to change app.config during runtime. The app.config should be used for program setup and configuration; not for things that are dynamic that the application is actually modifying.

Suggest you create seperate "KeyValue.xml" file for dynamic objects.

AdamBT
+1  A: 

After some more searching net, I've found a very good (albeit very long) article which describes in dept the .Net configuration model:

Unraveling the Mysteries of .NET 2.0 Configuration

I also found something very usefull:

Configuration Section Designer

A visual studio adding for visually designing config sections and generating the coresponging XSD files. I hope someone finds this useful.

Pop Catalin