在应用程序设置中保存词典
我想做这样的事情:
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");
我能以任何方式收到类似的行为吗?
谢谢 !
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 !
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以编写自己的配置部分 - 有很多 教程。
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:
You can then turn that into a nested dictionary in your code without too much effort, if you really need it like that.
使用
System.Configuration.NameValueSectionHandler
找到内置解决方案 在此链接下 谢谢大家!Found built in solution using
System.Configuration.NameValueSectionHandler
Under this link Thank you all!