Robbie Dee
Robbie Dee

Reputation: 1977

Extending key value pair functionality in app.config

I have a series of rules in my console application that I want to turn on and off in app.config.

App.config supports key value pairs, so good times - I can simply define some rule keys and store the flag in the value.

I now want to add a bespoke comment field I can read to get a rule description. However, there doesn't seem to be any facility to do this.

Clearly I could roll my own config file and read it with standard XML methods but I'm thinking there must be a better way of doing this within the app.config file.

I could also include the comment within the key/value but this too seems unsatisfactory.

Any ideas?

Upvotes: 3

Views: 1727

Answers (2)

Robbie Dee
Robbie Dee

Reputation: 1977

OK, following Pavel's initial lead and some code I got here, I managed to produce the following. Hope it is useful to other weary travellers! :-)

App.config

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections>
    <section name="RuleSet" type="ExtendedKVP.RuleSection, ExtendedKVP" />
  </configSections>
  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
  </startup>
  <RuleSet>
    <TheRules>
      <add ruleId="RL101" ruleActive="true" ruleDesc="Don't do this" />
      <add ruleId="RL202" ruleActive="false" ruleDesc="Avoid that" />
      <add ruleId="RL303" ruleActive="true" ruleDesc="Missing the other" />
    </TheRules>
  </RuleSet>
</configuration>

Program.cs

using System;
using System.Configuration;

namespace ExtendedKVP
{
    class Program
    {
        static void Main(string[] args)
        {           
            var connectionManagerDataSection = ConfigurationManager.GetSection(RuleSection.SectionName) as RuleSection;
            if (connectionManagerDataSection != null)
            {
                foreach (RuleElement element in connectionManagerDataSection.ConnectionManagerEndpoints)
                {
                    Console.WriteLine(string.Format("RuleId: {0}, RuleActive: {1}, RuleDesc: {2}", element.RuleId, element.RuleActive, element.RuleDesc));                
                }
            }
        }
    }
}

ConfigReader.cs

using System.Configuration;

namespace ExtendedKVP
{
    public class RuleSection : ConfigurationSection
    {       
        public const string SectionName = "RuleSet";

        private const string RuleCollectionName = "TheRules";

        [ConfigurationProperty(RuleCollectionName)]
        [ConfigurationCollection(typeof(RuleCollection), AddItemName = "add")]
        public RuleCollection ConnectionManagerEndpoints { get { return (RuleCollection)base[RuleCollectionName]; } }
    }

    public class RuleCollection : ConfigurationElementCollection
    {
        protected override ConfigurationElement CreateNewElement()
        {
            return new RuleElement();
        }

        protected override object GetElementKey(ConfigurationElement element)
        {
            return ((RuleElement)element).RuleId;
        }
    }

    public class RuleElement : ConfigurationElement
    {
        [ConfigurationProperty("ruleId", IsRequired = true)]
        public string RuleId
        {
            get { return (string)this["ruleId"]; }
            set { this["ruleId"] = value; }
        }

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

        [ConfigurationProperty("ruleActive", IsRequired = false, DefaultValue = false)]
        public bool RuleActive
        {
            get { return (bool)this["ruleActive"]; }
            set { this["ruleActive"] = value; }
        }       
    }
}

Upvotes: 4

Pavel Timoshenko
Pavel Timoshenko

Reputation: 721

You can create your own Configuration Section in app.config and use it for custom settings.

Click here for details.

Upvotes: 2

Related Questions