代码之家  ›  专栏  ›  技术社区  ›  Markus

C AppSettings:是否有一种简单的方法可以将集合放入<AppSetting>

  •  13
  • Markus  · 技术社区  · 16 年前

    我试过

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

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

    但我只有最后一个成员。 我怎么能轻易地解决这个问题?

    5 回复  |  直到 12 年前
        1
  •  24
  •   thomaux    12 年前

    我处理过一个类似的问题,我处理过这个代码。希望这对你的问题有所帮助。

    在这种情况下,列表(类似于我的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;
            }
        }
    
    
    
      }
    }
    

    配置集合

    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;
        }
     }
    }
    

    Web配置节

    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;
        }
     }
    }
    
        2
  •  3
  •   Bill H    14 年前
        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]);
        }
    
        3
  •  2
  •   Graeme Christie    12 年前

    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;        
       }
    
        4
  •  1
  •   Phil.Wheeler    16 年前

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

    唯一要做的就是创建一个单独的(静态的?)用于以与System.Configuration.ConfigurationManager.AppSettings类类似的方式读取XML的类。

    另一方面,如果它必须在web.config文件中,我建议实现这一点的唯一方法就是在一个“list”设置中使用一个以[pipe/comma/semi-colon]分隔的数组。

        5
  •  1
  •   ChirpingPanda    14 年前

    haacked提供了一种简洁的配置设置方法。他的方法使用了一个从配置节派生的类。因此,对于他的博客示例,您的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