A personal repository of technical notes. - CSC

ConfigurationManager.GetSection() as NameValueCollection Example

Problem
Need a custom AppSettings section in config file.

Solution
Create a custom section in config file based upon AppSettingsSection. Then use NameValueCollection to handle settings. See ConfigurationManager GetSection example below.

Note: Tested in .NET 4.0 and 2.0. If using 2.0, change the version number of type in config from 4 to 2.

xml version="1.0"?>
<configuration>
       <configSections>
              <sectionGroup name="customAppSettingsGroup">
                     <section name="customAppSettingsAAA" type="System.Configuration.AppSettingsSection, System.Configuration, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
                     <section name="customAppSettingsBBB" type="System.Configuration.AppSettingsSection, System.Configuration, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
                     <section name="customAppSettingsCCC" type="System.Configuration.AppSettingsSection, System.Configuration, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
              </sectionGroup>
       </configSections>
       <customAppSettingsGroup>
              <customAppSettingsAAA>
                     <add key="KeyOneAAA" value="ValueOneAAA"/>
                     <add key="KeyTwoAAA" value="ValueTwoAAA"/>
                     <add key="KeyThreeAAA" value="ValueThreeAAA"/>
              </customAppSettingsAAA>
              <customAppSettingsBBB>
                     <add key="KeyOneBBB" value="ValueOneBBB"/>
                     <add key="KeyTwoBBB" value="ValueTwoBBB"/>
                     <add key="KeyThreeBBB" value="ValueThreeBBB"/>
              </customAppSettingsBBB>
              <customAppSettingsCCC>
                     <add key="KeyOneCCC" value="ValueOneCCC"/>
                     <add key="KeyTwoCCC" value="ValueTwoCCC"/>
                     <add key="KeyThreeCCC" value="ValueThreeCCC"/>
              </customAppSettingsCCC>
       </customAppSettingsGroup>
</configuration>

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

public partial class ProofOfConcept_CustomAppSettings_Default : System.Web.UI.Page
{
       protected void Page_Load(object sender, EventArgs e)
       {
              // EXAMPLE 1: Retrieve 1 custom appsettings section.

              // ConfigurationManager.GetSection() as NameValueCollection example
              // Create a custom AppSettings section in config file
              NameValueCollection settingsAAA = ConfigurationManager.GetSection("customAppSettingsGroup/customAppSettingsAAA") as NameValueCollection;
              if (settingsAAA != null && settingsAAA.Count > 0)
              {
                     // Individual setting
                     Response.Write(settingsAAA["KeyTwoAAA"]);

                     // All settings' Values
                     foreach (string key in settingsAAA.AllKeys)
                     {
                           Response.Write(settingsAAA[key]);
                     }

                     // All settings' Keys and Values to GridView via Dictionary.
                     Dictionary<string, string> outputDictionary = new Dictionary<string, string>(allSettings.Count);
                     foreach (string key in allSettings.AllKeys)
                     {
                            outputDictionary.Add(key, allSettings[key]);
                     }
                     AllSettingsGridView.DataSource = outputDictionary;
                     AllSettingsGridView.DataBind();
              }

              // EXAMPLE 2: Retrieve multiple custom appsettings sections into a single Dictionary collection.

              Dictionary<string, NameValueCollection> allSettings = new Dictionary<string, NameValueCollection>();

              allSettings.Add("customAppSettingsAAA", ConfigurationManager.GetSection("customAppSettingsGroup/customAppSettingsAAA") as NameValueCollection);
              allSettings.Add("customAppSettingsBBB", ConfigurationManager.GetSection("customAppSettingsGroup/customAppSettingsBBB") as NameValueCollection);
              allSettings.Add("customAppSettingsCCC", ConfigurationManager.GetSection("customAppSettingsGroup/customAppSettingsCCC") as NameValueCollection);

              Response.Write(allSettings["customAppSettingsAAA"]["KeyTwoAAA"]);
              Response.Write(allSettings["customAppSettingsBBB"]["KeyOneBBB"]);
              Response.Write(allSettings["customAppSettingsCCC"]["KeyThreeCCC"]);
       }
}

Updates
2014-05-23 Added example: All settings' Keys and Values to GridView via Dictionary.
2013-12-05 Added Example 2.
2013-11-11 Updated example code.

No comments:

Post a Comment