How to read multiple values in C# app.config file?

10,237

Solution 1

I think you should implement a section.

I made some sample code that might be exactly what you want:

using System.Collections.Generic;
using System.Configuration;
using System.Linq;

namespace ConsoleApplication1
{
    public sealed class UsersConfigMapSection : ConfigurationSection
    {
        private static UsersConfigMapSection config = ConfigurationManager.GetSection("Users") as UsersConfigMapSection;

        public static UsersConfigMapSection Config
        {
            get
            {
                return config;
            }
        }

        [ConfigurationProperty("", IsRequired = true, IsDefaultCollection = true)]
        private UsersConfigMapConfigElements Settings
        {
            get { return (UsersConfigMapConfigElements)this[""]; }
            set { this[""] = value; }
        }

        public IEnumerable<UsersConfigMapConfigElement> SettingsList
        {
            get { return this.Settings.Cast<UsersConfigMapConfigElement>(); }
        }
    }

    public sealed class UsersConfigMapConfigElements : ConfigurationElementCollection
    {
        protected override ConfigurationElement CreateNewElement()
        {
            return new UsersConfigMapConfigElement();
        }
        protected override object GetElementKey(ConfigurationElement element)
        {
            return ((UsersConfigMapConfigElement)element).Username;
        }
    }

    public sealed class UsersConfigMapConfigElement : ConfigurationElement
    {
        [ConfigurationProperty("username", IsKey = true, IsRequired = true)]
        public string Username
        {
            get { return (string)base["username"]; }
            set { base["username"] = value; }
        }

        [ConfigurationProperty("password", IsRequired = true)]
        public string Password
        {
            get { return (string)base["password"]; }
            set { base["password"] = value; }
        }

        [ConfigurationProperty("domain", IsRequired = true)]
        public string Domain
        {
            get { return (string)base["domain"]; }
            set { base["domain"] = value; }
        }
    }
}

Then you extract users from your config file like this:

var users = UsersConfigMapSection.Config.SettingsList.ToList();

And finally your config file should look like this:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections>
    <section name="Users" type="ConsoleApplication1.UsersConfigMapSection, ConsoleApplication1"/>
  </configSections>
  <Users>
    <add username = "Dinesh" password ="Password" domain ="MyCompany" />
    <add username = "Kumar" password ="Password" domain ="MyCompany" />
  </Users>
    <startup> 
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
    </startup>
</configuration>

Solution 2

or you can use this work-around to achieve the same ...

<add key="username" value="A,B,C"/>
And

string[] mykey = ConfigurationManager.AppSettings["username"].Split(',');
Share:
10,237
Dineshkumar
Author by

Dineshkumar

Live let live.

Updated on June 09, 2022

Comments

  • Dineshkumar
    Dineshkumar almost 2 years

    I want to read the following app.config file.. How to read it? Do I need to change anything in order to read the file ??

    <?xml version="1.0" encoding="utf-8" ?>
    <configuration>
    <Users>
      <add username = "Dinesh" password ="Password" domain ="MyCompany" />
     <add username = "Kumar" password ="Password" domain ="MyCompany" />
    </Users>
    </configuration>
    
  • Dineshkumar
    Dineshkumar about 10 years
    In case two user names, how will I go ahead ?
  • Aftab Ahmed
    Aftab Ahmed about 10 years
    then you have to add another key for second user-name.
  • Aftab Ahmed
    Aftab Ahmed about 10 years
    no if you want to get another username from web.config you need to add another xml tag with different key e.g <add key="username" value="Dinesh" /> and <add key="username1" value="Kumar" /> because in code you will be accessing through keys not by value.