i recently wrote my first custom config section. I have a collection of index files, each has a path and a savetime:
internal class searchConfigSection : ConfigurationSection
{
static ConfigurationProperty s_indexCollection;
static ConfigurationPropertyCollection s_propertyCollection;
static openSearchConfigSection()
{
s_indexCollection = new ConfigurationProperty("indexCollection", typeof(IndexCollection), null, ConfigurationPropertyOptions.None);
s_propertyCollection = new ConfigurationPropertyCollection();
s_propertyCollection.Add(s_indexCollection);
}
protected override ConfigurationPropertyCollection Properties
{
get
{
return s_propertyCollection;
}
}
public IndexCollection IndexCollection
{
get { return (IndexCollection)base[s_indexCollection]; }
}
}
internal class IndexCollection : ConfigurationElementCollection
{
#region static fields
static ConfigurationPropertyCollection s_properties;
#endregion
#region constructors
static IndexCollection()
{
s_properties = new ConfigurationPropertyCollection();
}
public IndexCollection()
{
}
#endregion
#region override
public override ConfigurationElementCollectionType CollectionType
{
get { return ConfigurationElementCollectionType.BasicMap; }
}
protected override string ElementName
{
get { return "index"; }
}
protected override ConfigurationPropertyCollection Properties
{
get { return s_properties; }
}
protected override ConfigurationElement CreateNewElement()
{
return new IndexInformation();
}
protected override object GetElementKey(ConfigurationElement element)
{
return (element as IndexInformation).Path;
}
#endregion
#region indexer
public IndexInformation this[int index]
{
get { return (IndexInformation)base.BaseGet(index); }
set
{
if (base.BaseGet(index) != null)
{
base.BaseRemoveAt(index);
}
base.BaseAdd(index, value);
}
}
#endregion
#region modify
public void Add(IndexInformation element)
{
base.BaseAdd(element);
}
public void Remove(string path)
{
base.BaseRemove(path);
}
public void Remove(IndexInformation element)
{
base.BaseRemove(GetElementKey(element));
}
public void Clear()
{
base.BaseClear();
}
public void RemoveAt(int index)
{
base.BaseRemoveAt(index);
}
public string GetKey(int index)
{
return (string)base.BaseGetKey(index);
}
#endregion
}
internal class IndexInformation : ConfigurationElement
{
#region static fields
static ConfigurationProperty s_path;
static ConfigurationPropertyCollection s_properties;
static ConfigurationProperty s_saveTime;
#endregion
#region constructor
static IndexInformation()
{
s_path = new ConfigurationProperty("path", typeof(string), null, ConfigurationPropertyOptions.IsRequired);
s_saveTime = new ConfigurationProperty("saveTime", typeof(DateTime), null, ConfigurationPropertyOptions.IsRequired);
s_properties = new ConfigurationPropertyCollection();
s_properties.Add(s_path);
s_properties.Add(s_saveTime);
}
#endregion
#region properties
protected override ConfigurationPropertyCollection Properties
{
get { return s_properties; }
}
public string Path
{
get { return (string)base[s_path]; }
set { base[s_path] = value; }
}
public DateTime SaveTime
{
get { return (DateTime)base[s_saveTime]; }
set { base[s_saveTime] = value; }
}
#endregion
}
Here is the app.config file:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="searchConfig" type="Configuration.searchConfigSection,searchWindowsClient" />
</configSections>
<searchConfig>
<indexCollection>
<index path="C:\\index.xml" saveTime="" />
</indexCollecetion>
<searchConfig/>
</configuration>