views:

1512

answers:

2

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?

+2  A: 

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.

Phil.Wheeler
:S sux, i thougth there must be simular to the collections in a normal app.Config <aplicationConfiguration> tag
Markus
+1  A: 

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:

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






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


}
}
Beginner
very intersting thing i will try this and may ask for help if i dont get it
Markus
I tried this but i doenst works i get this error:When creating the configuration section handler for "URLSection" An error has occurred.: The file or assembly "A, Version = 1.0.0.0, Culture = neutral, PublicKeyToken = null 'or one of its dependencies was not foundBut why? i just copied your code
Markus
Markus I changed the Namespace (A) name in this code as it represented some stuff related to company I work in. Thus might be you are not able to build your code, but this design you can find in this link:http://msdn.microsoft.com/en-us/library/2tw134k3.aspx
Beginner