0

I would like to do something like this :

app.config :

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
 <appSettings>
   <setting1> 
  <add key ="key1" value ="value1"/>
  <add key ="key2" value ="value2"/>
</setting1>
<setting2>
  <add key ="key1" value ="value1"/>
  <add key ="key2" value ="value2"/>
</setting2>

my.cs:

Configuration config =
                    ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
var appSettings = config.AppSettings;

Assert.equals(appSettings.Settings["setting1"]["key1"] == "value1");
Assert.equals(appSettings.Settings["setting1"]["key2"] == "value2");
Assert.equals(appSettings.Settings["setting2"]["key1"] == "value1");
Assert.equals(appSettings.Settings["setting2"]["key2"] == "value2");

could I in any way receive similar behavior?

Thank You !

Sergey Kucher
  • 4,140
  • 5
  • 29
  • 47

3 Answers3

2

You can write your own configuration section - there are many tutorials around.

Oded
  • 489,969
  • 99
  • 883
  • 1,009
1

Simplest answer, don't and use a dot to "add a namespace" to the key:

<add key="setting1.key1" value="value1" />
<add key="setting1.key2" value="value2" />
<add key="setting2.key1" value="value3" />
<add key="setting2.key2" value="value4" />

You can then turn that into a nested dictionary in your code without too much effort, if you really need it like that.

Deleted
  • 4,804
  • 1
  • 22
  • 17
1

Found built in solution using System.Configuration.NameValueSectionHandler Under this link Thank you all!

Community
  • 1
  • 1
Sergey Kucher
  • 4,140
  • 5
  • 29
  • 47