Lucas
Lucas

Reputation: 3502

How to handle user settings?

I want to save a list of key-value pairs for a user in sections.

<section="SomeFoo">
   <key="foo" value="bar" />
</Section>

I want to read, add, edit and remove dynamically entries and sections. Also I want to get all keys of an section.

Something like

config.Read("SomeFoo", "foo");              // Read key foo from section SomeFoo
config.Read("SomeFoo");                     // Get all keys from section SomeFoo
config.Add("SomeFoo", "foo", "bar");        // Add key to section SomeFoo, key is foo, value is bar
config.Edit("SomeFoo", "foo", "newValue");  // Edit existing key foo from section SomeFoo and overwrite current value with newValue
config.Remove("SomeFoo", "foo");            // Remove key foo from section SomeFoo
config.Remove("SomeFoo");                   // Remove section SomeFoo

I've tried to use AppSettings and SectionGroups. See here. But it seems I can't add dynamically keys to a SectionGroup. Is that even possible? If it's not possible what alternatives do I have?

It is important that settings are saved for each user. %appdata%\foo\bar.config.

Upvotes: 0

Views: 113

Answers (1)

Daniel A. White
Daniel A. White

Reputation: 190942

I think you want a ConfigurationElementCollection.

http://msdn.microsoft.com/en-us/library/system.configuration.configurationelementcollection(v=vs.80).aspx

Upvotes: 0

Related Questions