开发者

Save Dictionary in application settings

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 !


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


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.


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜