C# AppSettings:有没有一种简单的方法将集合放入中?
我尝试了
<appSettings >
<add key="List" value="1"/>
<add key="List" value="2"/>
<add key="List" value="3"/>
</appSettings >
System.Configuration.ConfigurationManager.AppSettings.GetValues("List");
但我只得到了最后一个成员。 我怎样才能轻松解决这个问题?
i tried
<appSettings >
<add key="List" value="1"/>
<add key="List" value="2"/>
<add key="List" value="3"/>
</appSettings >
and System.Configuration.ConfigurationManager.AppSettings.GetValues("List");
But i only get the last member .
How could i solve this easily?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我处理过类似的问题,并用这段代码做到了。希望这对您的问题有所帮助。
在这种情况下,列表(类似于我的 URLSection)将在 web.config 中具有完整的配置部分,然后您可以从该部分获取所有值。
我为此创建了三个类:ConfigElement、ConfigElementCollection、WebConfigSection。
ConfigElement
ConfigElementCollection
WebConfigSection
I have dealt a similar issue and I did it with this code. Hope this helps in your problem.
In this case List (similar to my URLSection) will have a full configuration Section in web.config which you can get all values from this section then.
I made three classes for this: ConfigElement, ConfigElementCollection, WebConfigSection.
ConfigElement
ConfigElementCollection
WebConfigSection
NinjaSettings 开箱即用。
在包管理器控制台中,
您将声明您的列表,
然后创建一个接口,其中列表映射到任何 ICollection 或数组,
然后使用 NinjaSettings 包装器访问您的设置。通常您会使用 IOC 连接它,但基本用法是
NinjaSettings does this out of the box.
In the package manager console
You would declare your list as
then create an Interface with a mapping for list to any ICollection or Array
then access your settings user the NinjaSettings wrapper. Generally you would wire this up using IOC, but the basic usage is
您最好将此信息放在单独的 XML 文件中,并在 AppSettings 中引用该文件。这将使您在检索和使用信息的方式方面拥有更大的灵活性。
唯一的问题是您需要创建一个单独的(静态?)类,用于以与 System.Configuration.ConfigurationManager.AppSettings 类类似的方式读取 XML。
另一方面,如果它必须位于您的 Web.Config 文件中,我建议实现此目的的唯一方法就是在一个“列表”设置中使用 [管道/逗号/分号] 分隔数组。
You'd likely be better off putting this information in a separate XML file and having a reference to that file in AppSettings. That would give you a lot more flexibility around how you retrieved the information and consumed it.
The only thing would be that you'd want to create a separate (static?) class for reading the XML in a similar fashion to the System.Configuration.ConfigurationManager.AppSettings class.
If, on the other hand, it HAD to be in your Web.Config file, I would suggest the only way to achieve this simply would be to have a [pipe/comma/semi-colon] delimited array in one "List" setting.
Haacked 提供了一种简洁的配置设置方法。他的方法使用一个派生自 ConfigurationSection 的类。因此,对于他的博客示例,您的 app.config 或 web.config xml 表示将如下所示:
这值得一读:
http://haacked.com/archive/2007/03/12/custom-configuration-sections-in-3-easy-steps.aspx
Haacked provides a concise approach to configuration settings. His approach uses one class deriving from ConfigurationSection. So for his blog example your app.config or web.config xml representation will look like this:
This is worth a read:
http://haacked.com/archive/2007/03/12/custom-configuration-sections-in-3-easy-steps.aspx