How to Create Custom Section in Config File

Sample demonstrates creating and reading from Custom sections in config file using System.Collections.Specialized

ITPro Today

April 20, 2004

3 Min Read
ITPro Today logo

Sample demonstrates creating and reading from Custom sections in config file usingSystem.Collections.Specialized

 

We can create custom sections in application config files. This will be handy when you have large blocks of data to be grouped into sections. See the following sample config file. We have created a section called 'CuteUrls' and have added few key value pairs in this section.

 

Code sample for you…

 

                        type="System.Configuration.NameValueSectionHandler" />                                          The following C# code can be used to retrieve the values from the above custom config section. Declare the  namespace on top of your file, so that you don't need to type the fully qualified class names in the code.Now use this code to read values from our custom section….


NameValueCollection section = (NameValueCollection)                                  ConfigurationSettings.GetConfig("FavoriteUrls"); for (int i = 0; i < section.Keys.Count; i++){    string key = section.Keys[i];    string name = section.GetValues (key)[0];     // .GetValues() returns a collection of values. In our case, we have     //only one value, so get the first one.}  

Sign up for the ITPro Today newsletter
Stay on top of the IT universe with commentary, news analysis, how-to's, and tips delivered to your inbox daily.

You May Also Like