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

发布于 2024-11-28 08:40:00 字数 917 浏览 0 评论 0原文

我想做这样的事情:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

贪了杯 2024-12-05 08:40:01

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

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

蓝眼睛不忧郁 2024-12-05 08:40:01

最简单的答案是,不要使用点来“添加命名空间”到键中:

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

如果您确实需要这样,那么您可以将其转换为代码中的嵌套字典,而无需太多努力。

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.

计㈡愣 2024-12-05 08:40:01

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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文