Adding application configuration in app.config, especially under the appSetting key is something which every developer is used to.
<appSettings> <add key="Version" value="1.0" /> <add key="AutoSave" value="True" /> <add key="LoadOnWindowsStartUp" value="True" /> </appSettings>
What if we need to add our own customer collection. For example,
<PluginSettings> <Plugins> <Plugin ID="906260f8-57ab-417a-8d83-015c73f4a5c8" IsEnabled="true"></Plugin> <Plugin ID="906260f8-57ab-417a-8d83-015c73f4a5c9" IsEnabled="true"></Plugin> </Plugins> </PluginSettings>
That’s when we need to write up code to define these customized segments. We will begin with the basic element first, which in this case is
<Plugin ID="906260f8-57ab-417a-8d83-015c73f4a5c8" IsEnabled="true"></Plugin>
As seen, the basic element has two properties, a GUID named ID and a boolean value called IsEnabled. Let’s go ahead and define them. Our custom class needs to inherit from ConfigurationElement Class.
public class PluginConfigurationElement:ConfigurationElement { private const string XML_PLUGIN_ID = "ID"; private const string XML_TITLE = "Title"; private const string XML_IS_ENABLED = "IsEnabled"; [ConfigurationProperty(XML_PLUGIN_ID,IsRequired =true,IsKey =true)] public Guid ID { get { return (Guid)this[XML_PLUGIN_ID]; } set { this[XML_PLUGIN_ID] = value; } } [ConfigurationProperty(XML_IS_ENABLED, IsRequired = true, DefaultValue =false)] public bool IsEnabled { get { return (bool)this[XML_IS_ENABLED]; } set { this[XML_IS_ENABLED] = value; } } }
As seen in the example configuration, what we need is a collection of the defined Basic element. We will go ahead and define the collection now. Similar to basic element, we have a base class to inherit from.
public class PluginCollection:ConfigurationElementCollection { internal const string PropertyName = "Plugin"; public override ConfigurationElementCollectionType CollectionType { get { return ConfigurationElementCollectionType.BasicMapAlternate; } } protected override string ElementName { get { return PropertyName; } } protected override bool IsElementName(string elementName)= elementName.Equals(PropertyName, StringComparison.InvariantCultureIgnoreCase); public override bool IsReadOnly() = false; protected override ConfigurationElement CreateNewElement()= new PluginConfigurationElement(); protected override object GetElementKey(ConfigurationElement element) = ((PluginConfigurationElement)(element)).ID; protected override void BaseAdd(ConfigurationElement element) = base.BaseAdd((PluginConfigurationElement)element); public void Add(PluginConfigurationElement element) = this.BaseAdd(element); public void Remove(Guid Key) = this.BaseRemove(Key); public void Clear() = this.BaseClear(); public PluginConfigurationElement this[int idx] { get { return (PluginConfigurationElement)BaseGet(idx); } } }
The final class we need to define is the actual Section itself.
public class PluginSection:ConfigurationSection { private const string XML_PLUGINS = "Plugins"; [ConfigurationProperty(XML_PLUGINS)] public PluginCollection Plugins { get { return ((PluginCollection)(base[XML_PLUGINS])); } set { base[XML_PLUGINS] = value; } } }
We are almost ready now. The final step required is to reference the class in app.config.
<configSections> <section name="PluginSettings" type="MyNamespace.PluginSection, MyNamespace " /> </configSections>
Make sure you put this as the first step in app.config, right below the root element . Now you are all set to write your own configuration. Happy Coding.