Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# AppSettings: Is there a easy way to put a collection into <appSetting>

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?

like image 814
Markus Avatar asked Nov 18 '09 11:11

Markus


4 Answers

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.

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

I made three classes for this: ConfigElement, ConfigElementCollection, WebConfigSection.

ConfigElement

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;
    }
 }
}
like image 169
Ashish Jain Avatar answered Sep 30 '22 01:09

Ashish Jain


    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]);
    }
like image 42
Bill H Avatar answered Sep 30 '22 01:09

Bill H


NinjaSettings does this out of the box.

In the package manager console

Install-Package NinjaSettings

You would declare your list as

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

then create an Interface with a mapping for list to any ICollection or Array

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

then access your settings user the NinjaSettings wrapper. Generally you would wire this up using IOC, but the basic usage is

   var settings = new NinjaSettings<IAppSettings>().Settings;

   int total = 0;
   for (var i in settings.List) 
   {
      total+=i;        
   }
like image 33
Graeme Christie Avatar answered Sep 30 '22 01:09

Graeme Christie


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.

like image 44
Phil.Wheeler Avatar answered Sep 29 '22 23:09

Phil.Wheeler