Subscribe to Subscribers

Resources » .NET programming » .NET Framework

How to use custom sections in config file


Posted Date:     Category: .NET Framework    
Author: Member Level: Gold    Points: 10


Web.config and App.config files are the best places to store application specific data. This sample demonstrates creating and reading from custom sections in config file.



You 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 'FavoriteUrls' and have added few key value pairs in this section.

<?xml version="1.0" encoding="Windows-1252"?>
<configuration>
<configSections>
<section name="FavoriteUrls"
type="System.Configuration.NameValueSectionHandler" />
</configSections>



<appSettings>
</appSettings>

<FavoriteUrls>
<add key="Microsoft" value="http://www.microsoft.com/" />
<add key="DotNetSpider" value="http://www.DotNetSpider.com/" />
<add key="AsianSpider" value="http://www.AsianSpider.com/" />
</FavoriteUrls>

</configuration>

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.

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


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.
}


Feel free to share your feedback on this.





Did you like this resource? Share it with your friends and show your love!


Responses to "How to use custom sections in config file"
Author: Tamim Sadikali    29 Jul 2004Member Level: Bronze   Points : 0
Tony,

On someone asking this question you directed them to your 'creating custom config sections' entry. This does not work if you want to call the app.config/web.config from within the class library. Any other thoughts??



Author: Tamim Sadikali    29 Jul 2004Member Level: Bronze   Points : 0
Tony,

On someone asking this question you directed them to your 'creating custom config sections' entry. This does not work if you want to call the app.config/web.config from within the class library. Any other thoughts??



Author: Tony John    29 Jul 2004Member Level: Gold   Points : 0
Reading the app.config from class library is no different from reading it from anywhere else. You can use the same syntax.

The only thing is, you cannot have an app.config/web.config for a class library. The config file should be attached to the main application. The class library cannot run on its own. It will always run as part of another executable. So, if you use ConfigurationSettings.AppSettings["Key"] from a class library, it will look for the Key in the entry point application's config file.

In case of web applications, you can have all the keys in the root web.config and all class libraries can read from it.



Author: Gyanendra Kumar Nayak    16 Sep 2008Member Level: Silver   Points : 1
The example posted here is very simple to understand the logic. Keep it up...Thanks a lot for this


Author: Bhavin    19 Jun 2010Member Level: Bronze   Points : 2
Its great to see this.....as a student i hv used it in my project also...now i want to add sectionGroup of own section...
eg.



value="Welcome to HR Department" />
value="________" />


value="Welcome to Marketing Department" />
value="_____________" />



so how do i achieve d same?
however i hv tried but it saya dat "could nt found information for Department" & same for inner level tag also
I think m missing type of sectionGroup or of any others



Guest Author: Ofer     24 May 2012
Very Good!!!


Guest Author: Alex     26 Aug 2012
oh !

I've been looking for this one day, and you did it in a few lines.
While the others were complex things.
I love you.



Author: Dharmaraj Nagarajan    27 Feb 2013Member Level: Gold   Points : 5
Tony,

I noticed when I tried adding custom section in App.config file and retrieving values from App.config got below error.

Error Message
Could not load type 'System.Configuration.NameValueSelectionHandler' from assembly 'System.Configuration, Version=2.0.0.0

Workaround
Need to add the below line in App.config file, when defining the custom section. These will help in avoiding Error.

type="System.Configuration.NameValueSectionHandler,System, Version=2.0.0.0, Culture=neutral,
PublicKeyToken=b77a5c561934e089"

I think this will help users who experiencing error.
Note
These error I had noticed when running in Windows 7 machine with Home Edition, not sure every one will face the issue. Happy coding.



Feedbacks      

Post Comment:




  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:   Sign In to fill automatically.
    Email: (Will not be published, but required to validate comment)



    Type the numbers and letters shown on the left.


    Next Resource: How to use System.Convert class to convert a base data type to another base data type.
    Previous Resource: Using ArrayList
    Return to Resources
    Post New Resource
    Category: .NET Framework


    Post resources and earn money!
     
    More Resources
    Popular Tags   Tag posting guidelines   Search Tags  
    (No tags found.)

    Active Members
    TodayLast 7 Daysmore...

    Awards & Gifts
    Talk to Webmaster Tony John
    Copyright © SpiderWorks Technologies Pvt Ltd., Kochi, India
    2005 - 2013 All Rights Reserved.
    .NET and other trademarks mentioned in this site belong to Microsoft and other respective trademark owners.
    Articles, tutorials and all other content offered here is for educational purpose only.
    We are not associated with Microsoft or its partners.