Created
December 17, 2013 15:41
-
-
Save mombrea/8006932 to your computer and use it in GitHub Desktop.
An example of a custom configSection handler for .NET
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Configuration; | |
using System.IO; | |
using System.Linq; | |
using System.Text; | |
using System.Threading.Tasks; | |
namespace Example | |
{ | |
public class FileRetriever | |
{ | |
public static FileRetrieverSection _Config = ConfigurationManager.GetSection("fileRetriever") as FileRetrieverSection; | |
public static FileElementCollection GetFiles() | |
{ | |
return _Config.Files; | |
} | |
} | |
public class FileRetrieverSection : ConfigurationSection | |
{ | |
[ConfigurationProperty("files")] | |
public FileElementCollection Files | |
{ | |
get { return (FileElementCollection)this["files"]; } | |
} | |
} | |
[ConfigurationCollection(typeof(FileElement))] | |
public class FileElementCollection : ConfigurationElementCollection | |
{ | |
public FileElement this[int index] | |
{ | |
get { return (FileElement)BaseGet(index); } | |
set | |
{ | |
if (BaseGet(index) != null) | |
BaseRemoveAt(index); | |
BaseAdd(index, value); | |
} | |
} | |
protected override ConfigurationElement CreateNewElement() | |
{ | |
return new FileElement(); | |
} | |
protected override object GetElementKey(ConfigurationElement element) | |
{ | |
return ((FileElement)element).Name; | |
} | |
} | |
public class FileElement : ConfigurationElement | |
{ | |
public FileElement() { } | |
[ConfigurationProperty("name",DefaultValue="", IsKey = true, IsRequired = true)] | |
public string Name | |
{ | |
get { return (string)this["name"]; } | |
set { this["name"] = value; } | |
} | |
[ConfigurationProperty("sourcePath",DefaultValue="", IsRequired = true)] | |
public string SourcePath | |
{ | |
get { return (string)this["sourcePath"]; } | |
set { this["sourcePath"] = value; } | |
} | |
[ConfigurationProperty("destinationPath",DefaultValue="", IsRequired = true)] | |
public string DestinationPath | |
{ | |
get { return (string)this["destinationPath"]; } | |
set { this["destinationPath"] = value; } | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment