30 April 2010

How to create custom configuration section

To create custom configuration section on a web.config or app.config file the attribute ConfigurationCollectionAttribute must be used.

In my example let us create a custom section named mails. It has the mails collection to send messages.


<configuration>
<configSections>
<section name="Mails" type="Samples.XProg.MailsSection, ConfigurationCollectionAttribute" />
</configSections>
<Mails>
<sendlist>
<clear />
<add name="mail1" mail="mail1@noip.com" />
<add name="mail2" mail="mail2@noip.com" />
</sendlist>
</Mails>
</configuration>


using System;
using System.Configuration;
 
namespace Samples.XProg
{
class TestMailConfigurationSection
{
static void Main(string[] args)
{
MailsSection mails = ConfigurationManager.GetSection("MailsSection") as MailsSection;
 
Console.WriteLine("Mails list:");
for (int i = 0; i < mails.SendList.Count; i++)
{
Console.WriteLine("Name={0} Mail{1}", mails.SendList[i].Name, mails.SendList[i].Mail);
}
 
Console.ReadLine();
}
}
 
public class MailsSection : ConfigurationSection
{
[ConfigurationProperty("sendlist", IsDefaultCollection = false)]
[ConfigurationCollection(typeof(SendListCollection), AddItemName = "add", ClearItemsName = "clear", RemoveItemName = "remove")]
public SendListCollection SendList
{
get
{                    
return (SendListCollection)base["sendlist"];;
}
}
 
}
 
 
public class SendListCollection : ConfigurationElementCollection
{
public UrlsCollection()
{
}
 
public override ConfigurationElementCollectionType CollectionType
{
get
{
return ConfigurationElementCollectionType.AddRemoveClearMap;
}
}
 
protected override ConfigurationElement CreateNewElement()
{
return new UrlConfigElement();
}
 
protected override Object GetElementKey(ConfigurationElement element)
{
return ((UrlConfigElement)element).Name;
}
 
public MailElement this[int index]
{
get
{
return (MailElement)BaseGet(index);
}
set
{
if (BaseGet(index) != null)
{
BaseRemoveAt(index);
}
BaseAdd(index, value);
}
}
 
new public MailElement this[string Name]
{
get
{
return (MailElement)BaseGet(Name);
}
}
 
public int IndexOf(MailElement mail)
{
return BaseIndexOf(mail);
}
 
public void Add(MailElement mail)
{
BaseAdd(mail);
}
protected override void BaseAdd(ConfigurationElement element)
{
BaseAdd(element, false);
}
 
public void Remove(MailElement mail)
{
if (BaseIndexOf(mail) >= 0)
BaseRemove(mail.Name);
}
 
public void RemoveAt(int index)
{
BaseRemoveAt(index);
}
 
public void Remove(string name)
{
BaseRemove(name);
}
 
public void Clear()
{
BaseClear();
}
}
 
 
public class MailElement : ConfigurationElement
{
public MailElement(String name, String mail)
{
this.Name = name;
this.Mail = mail;
}
 
public MailElement ()
{
}
 
[ConfigurationProperty("name", IsRequired = true, IsKey = true)]
public string Name
{
get
{
return (string)this["name"];
}
set
{
this["name"] = value;
}
}
 
[ConfigurationProperty("mail", DefaultValue = "http://www.microsoft.com", IsRequired = true)]
public string Mail
{
get
{
return (string)this["mail"];
}
set
{
this["mail"] = value;
}
}
 
}
}

No comments: