简体   繁体   English

在应用程序设置中保存字典

[英]Save Dictionary in application settings

I would like to do something like this : 我想做这样的事情:

app.config : 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: 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 ! 谢谢 !

您可以编写自己的配置部分-周围有许多 教程

使用System.Configuration.NameValueSectionHandler找到内置解决方案在此链接下谢谢大家!

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. 然后,如果确实需要它,则无需花费太多精力即可将其转换为代码中的嵌套字典。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM