Joseph Michael Pesch
VP Programming

ASP.Net Custom Configuration Setting

by 19. August 2008 16:50

Sample Code

using System.Collections;
using System.Collections.Generic;
using System.Collections.Specialized;

void Test()
{
  IDictionary singleTag = (IDictionary)ConfigurationSettings.GetConfig("MySingleTagSection");
   NameValueCollection nameValue = (NameValueCollection)ConfigurationSettings.GetConfig("MyNameValueSection");
   Hashtable dictionary = (Hashtable)ConfigurationSettings.GetConfig("MyDictionarySection");
   NameValueCollection nameValueGroup = (NameValueCollection)ConfigurationSettings.GetConfig("MySectionGroup/MySection1");
   System.Diagnostics.Debug.WriteLine((string)singleTag["sample1"]);
   System.Diagnostics.Debug.WriteLine((string)nameValue["key1"]);
   System.Diagnostics.Debug.WriteLine((string)dictionary["key1"]);
   System.Diagnostics.Debug.WriteLine((string)nameValueGroup["key1"]);
}

 


App.Config

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections>
    <section name="MySingleTagSection" type="System.Configuration.SingleTagSectionHandler"/>
    <section name="MyDictionarySection" type="System.Configuration.DictionarySectionHandler"/>
    <section name="MyNameValueSection" type="System.Configuration.NameValueSectionHandler"/>
    <sectionGroup name="MySectionGroup">
      <section name="MySection1" type="System.Configuration.NameValueSectionHandler"/>
      <section name="MySection2" type="System.Configuration.NameValueSectionHandler"/>
    </sectionGroup>
  </configSections>
  <MySingleTagSection sample1="value1" sample2="value2" sample3="value3"/>
  <MyDictionarySection>
    <add key="key1" value="value1"/>
    <add key="key2" value="value2"/>
  </MyDictionarySection>
  <MyNameValueSection>
    <add key="key1" value="value1"/>
    <add key="key2" value="value2"/>
  </MyNameValueSection>
  <MySectionGroup>
    <MySection1>
      <add key="key1" value="value1"/>
      <add key="key2" value="value2"/>
    </MySection1>
    <MySection2>
      <add key="key1" value="value1"/>
      <add key="key2" value="value2"/>
    </MySection2>
  </MySectionGroup>
</configuration>

Tags:

[None]

Comments are closed