我想这样做:
应用程序。配置:
<?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");
我能以任何方式收到类似的行为吗?
您可以编写自己的配置部分-周围有许多教程
最简单的答案,不要,并使用点来"添加命名空间":
<add key="setting1.key1" value="value1" />
<add key="setting1.key2" value="value2" />
<add key="setting2.key1" value="value3" />
<add key="setting2.key2" value="value4" />
如果你真的需要的话,你可以毫不费力地把它变成代码中的一个嵌套字典。
使用System.Configuration.NameValueSectionHandler
找到内置解决方案在此链接下谢谢大家!