【问题标题】:C# AppSettings: Is there a easy way to put a collection into <appSetting>C# AppSettings:有没有一种简单的方法可以将集合放入 <appSetting>
【发布时间】:2010-12-17 20:17:35
【问题描述】:

我试过了

<appSettings >
    <add key="List" value="1"/>
    <add key="List" value="2"/>
    <add key="List" value="3"/>
  </appSettings >

System.Configuration.ConfigurationManager.AppSettings.GetValues("List");

但我只得到最后一个成员。 我怎样才能轻松解决这个问题?

【问题讨论】:

标签: c# set collections setting


【解决方案1】:

您最好将此信息放在单独的 XML 文件中,并在 AppSettings 中引用该文件。这将使您在如何检索和使用信息方面更加灵活。

唯一的事情是您希望创建一个单独的(静态?)类,以类似于 System.Configuration.ConfigurationManager.AppSettings 类的方式读取 XML。

另一方面,如果它必须在您的 Web.Config 文件中,我建议实现此目的的唯一方法是将 [pipe/comma/semi-colon] 分隔数组放在一个“列表”设置。

【讨论】:

  • :S sux,我认为在正常的 app.Config 标签中必须有类似的集合
【解决方案2】:

我已经处理了一个类似的问题,我用这段代码做到了。希望这对您的问题有所帮助。

在这种情况下,List(类似于我的 URLSection)将在 web.config 中有一个完整的配置部分,然后您可以从该部分获取所有值。

<configSections>
    <section name="URLSection" type="A.WebConfigSection,A,Version=1.0.0.0,Culture=neutral,PublicKeyToken=null"/>
</configSections>

<appSettings></appSettings>

<URLSection>
    <urlCollection>
        <add url="1" value="a"/>
        <add url="2" value="b"/>
    </urlCollection>
</URLSection>

我为此创建了三个类:ConfigElement、ConfigElementCollection、WebConfigSection。

配置元素

using System;
using System.Collections.Generic;
using System.Text;
using System.Configuration;

namespace A
{
  public class ConfigElement:System.Configuration.ConfigurationElement
{
    [ConfigurationProperty("url",IsRequired=true) ]
    public string url
    {
        get
        {
            return this["url"] as string;
        }
    }

    [ConfigurationProperty("value", IsRequired = true)]
    public string value
    {
        get
        {
            return this["value"] as string;
        }
    }



  }
}

ConfigElementCollection

using System;
using System.Collections.Generic;
using System.Text;
using System.Configuration;

namespace A
{
  public class ConfigElementCollection:ConfigurationElementCollection
 {
    public ConfigElement this[int index]
    {
        get
        {
            return base.BaseGet(index) as ConfigElement;
        }

    }

    protected override ConfigurationElement CreateNewElement()
    {
        return new ConfigElement();
    }

    protected override object GetElementKey(ConfigurationElement element)
    {
        return ((ConfigElement)(element)).url;
    }
 }
}

WebConfigSection

using System;
using System.Collections.Generic;
using System.Text;
using System.Configuration;

namespace A
{
 public class WebConfigSection:ConfigurationSection
 {

    public WebConfigSection()
    {

    }

    [ConfigurationProperty("urlCollection")]
    public ConfigElementCollection allValues
    {
        get
        {
            return this["urlCollection"] as ConfigElementCollection;
        }
    }

    public static WebConfigSection GetConfigSection()
    {
        return ConfigurationSettings.GetConfig("URLSection") as WebConfigSection;
    }
 }
}

【讨论】:

  • 非常有趣的事情我会试试这个,如果我不明白可能会寻求帮助
  • 我试过这个,但我没有工作我得到这个错误:为“URLSection”创建配置节处理程序时发生错误。:文件或程序集“A,版本 = 1.0.0.0,文化= Neutral, PublicKeyToken = null '或者它的依赖项之一没有找到但是为什么?我只是复制了你的代码
  • Markus 我更改了此代码中的命名空间 (A) 名称,因为它代表了与我工作的公司相关的一些内容。因此可能是您无法构建代码,但您可以找到此设计在此链接中:msdn.microsoft.com/en-us/library/2tw134k3.aspx
  • 你能添加示例代码如何从配置文件中检索 urlCollection 吗?
  • @Bronek var configUrls = WebConfigSection.GetConfigSection();
【解决方案3】:

Haacked 提供了一种简洁的配置设置方法。他的方法使用一个派生自 ConfigurationSection 的类。因此,对于他的博客示例,您的 app.config 或 web.config xml 表示将如下所示:

<configuration>
  <configSections>
    <section name="BlogSettings" type="Fully.Qualified.TypeName.BlogSettings,   
      AssemblyName" />
  </configSections>
  <BlogSettings frontPagePostCount="10" title="You’ve Been Haacked" />
</configuration>

值得一读:

http://haacked.com/archive/2007/03/12/custom-configuration-sections-in-3-easy-steps.aspx

【讨论】:

    【解决方案4】:
        foreach (string str in ConfigurationManager.AppSettings.AllKeys)
        {
            if (str.ToUpper().IndexOf("SOMESPECIAL") > -1) //the somespecial ones you want to add in
                lstList.Add(ConfigurationManager.AppSettings[str]);
        }
    

    【讨论】:

    • +1 有点老套,但为我节省了一些时间来处理简单应用程序的自定义配置部分。
    • 这仅在键是唯一的情况下才有效,即 SOMESPECIAL-01、SOMESPECIAL-02 等。如果您只是重复 SOMESPECIAL,您只能得到最后一个。
    • @PaulGrimshaw 确实很hacky,也有Turkish İ problem。你最好使用 StringComparisonString.StartsWith 的重载。
    【解决方案5】:

    NinjaSettings 开箱即用。

    在包管理器控制台中

    Install-Package NinjaSettings
    

    您可以将您的列表声明为

      <appSettings>
        <add key="List" value="50,20,10,100"/>
      </appSettings>
    

    然后创建一个接口,将列表映射到任何 ICollection 或数组

    public interface IAppSettings
    {
        List<int> List { get; }
    }
    

    然后访问您的设置用户 NinjaSettings 包装器。通常你会使用 IOC 来连接它,但基本用法是

       var settings = new NinjaSettings<IAppSettings>().Settings;
    
       int total = 0;
       for (var i in settings.List) 
       {
          total+=i;        
       }
    

    【讨论】:

      猜你喜欢
      • 2012-04-30
      • 1970-01-01
      • 2010-09-06
      • 2011-01-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多