Skip to content

Instantly share code, notes, and snippets.

@Axemasta
Created March 7, 2019 09:39
Show Gist options
  • Save Axemasta/a3b8eff07caf5014a5f0143b106a42d5 to your computer and use it in GitHub Desktop.
Save Axemasta/a3b8eff07caf5014a5f0143b106a42d5 to your computer and use it in GitHub Desktop.
Custom App Config Sections
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<section name="excludeKeywords" type="YourNameSpace.SectionHandler, YourNamespace" />
</configSections>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5"/>
</startup>
<appSettings>
<add key="Test" value="Test value!"/>
</appSettings>
<!--<includeRegex>
<regex pattern=""/>
</includeRegex>-->
<!--Objects can be stored & deserialized in the section handler-->
<yourSectionName>
<item value="4097821023436939"/>
<item value="4010864067044940"/>
</yourSectionName>
</configuration>
using System.Collections.Generic;
using System.Configuration;
using System.Xml;
public class ExcludeKeywordSection : IConfigurationSectionHandler
{
public object Create(object parent, object configContext, XmlNode section)
{
List<string> myConfigObject = new List<string>();
foreach (XmlNode childNode in section.ChildNodes)
{
foreach (XmlAttribute attrib in childNode.Attributes)
{
myConfigObject.Add(attrib.Value);
}
}
return myConfigObject;
}
}
#region Properties
#region - App Config Properties
public static List<string> ExampleList
{
get
{
try
{
List<string> list = (List<string>)ConfigurationManager.GetSection("yourSectionName");
return list;
}
catch (Exception)
{
return null;
}
}
}
#endregion - App Config Properties
#endregion - Properties
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment