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.Generic;
using System.Collections.Specialized;
using System.Configuration;
using System.Text;
using System.Web.Configuration;

public partial class ProofOfConcept_Configuration_Default : System.Web.UI.Page
{
       protected void Page_Load(object sender, EventArgs e)
       {
              StringBuilder outputStringBuilder = new StringBuilder();

              // 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
                     outputStringBuilder.AppendLine("Individual setting ----------");
                     outputStringBuilder.AppendLine(settingsAAA["KeyTwoAAA"]);

                     // All settings Values in section
                     outputStringBuilder.AppendLine("All settings values in section ----------");
                     foreach (string key in settingsAAA.AllKeys)
                     {
                           outputStringBuilder.AppendLine(settingsAAA[key]);
                     }

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

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

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

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

              outputStringBuilder.AppendLine("Dictionary items ----------");
              outputStringBuilder.AppendLine(settingsDictionary["customAppSettingsAAA"]["KeyTwoAAA"]);
              outputStringBuilder.AppendLine(settingsDictionary["customAppSettingsBBB"]["KeyOneBBB"]);
              outputStringBuilder.AppendLine(settingsDictionary["customAppSettingsCCC"]["KeyThreeCCC"]);

              // EXAMPLE 3: Loop through each setting of each section in a group

              Configuration webConfig = WebConfigurationManager.OpenWebConfiguration("~/ProofOfConcept/Configuration"); // NOTE: MUST INCLUDE FULL PATH TO SUBFOLDER OF WEB.CONFIG IF NECESSARY
              ConfigurationSectionGroup myGroup = webConfig.GetSectionGroup("customAppSettingsGroup");
              outputStringBuilder.AppendLine("Loop through entire group ----------");

              foreach (ConfigurationSection section in myGroup.Sections)
              {
                     outputStringBuilder.AppendLine(section.SectionInformation.SectionName);
                     NameValueCollection settings = ConfigurationManager.GetSection(section.SectionInformation.SectionName) as NameValueCollection;
                     foreach (string key in settings)
                     {
                           outputStringBuilder.AppendLine(String.Format(" {0} = {1}", key, settings[key]));
                     }
              }

              MainLiteral.Text = outputStringBuilder.ToString();
       }
}

Sample Output

Individual setting ----------
ValueTwoAAA
All settings values in section ----------
ValueOneAAA
ValueTwoAAA
ValueThreeAAA
Dictionary items ----------
ValueTwoAAA
ValueOneBBB
ValueThreeCCC
Loop through entire group ----------
customAppSettingsGroup/customAppSettingsCCC
 KeyOneCCC = ValueOneCCC
 KeyTwoCCC = ValueTwoCCC
 KeyThreeCCC = ValueThreeCCC
customAppSettingsGroup/customAppSettingsAAA
 KeyOneAAA = ValueOneAAA
 KeyTwoAAA = ValueTwoAAA
 KeyThreeAAA = ValueThreeAAA
customAppSettingsGroup/customAppSettingsBBB
 KeyOneBBB = ValueOneBBB
 KeyTwoBBB = ValueTwoBBB
 KeyThreeBBB = ValueThreeBBB

KeyValue
[Dictionary to GridView][Example]
KeyOneAAAValueOneAAA
KeyTwoAAAValueTwoAAA
KeyThreeAAAValueThreeAAA


Updates
2020-04-01 Added example 3: Loop through each setting of each section in a group
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