How to Create Custom Section in Config File
Sample demonstrates creating and reading from Custom sections in config file using System.Collections.Specialized
April 20, 2004
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.}
About the Author
You May Also Like