Click here to Skip to main content
Click here to Skip to main content

Access Web.Config application settings from a console or Windows application

, 30 Jun 2004
Rate this:
Please Sign up or sign in to vote.
Example of how to read web.config settings into a console or Windows application.

Sample screenshot

Introduction

This code provides an easy to use mechanism for reading AppSettings values from the Web.Config in non web based environments. Additionally there is a very simple form that demonstrates the code being used.

Background

I have worked on many solutions that have comprised of Web projects together with Console or Windows applications. Often the need has arisen for the non web components to be able to read values from the Web.Config file (particularly in automated testing environments) so I wrote a class to simplify this and present it here.

Using the code

The code is designed to be very close in syntax to the usual method used for accessing web.config from a web app. Pass the constructor the location of the web.config file you wish to parse and then use the AppSettings method to obtain the desired value; an exception will be thrown should it not be found

string filename = @"c:\temp\Web.Config"; 
UK.Org.Webman.ConfigurationSettings ConfigurationSettings = 
    new UK.Org.Webman.ConfigurationSettings(filename); 
string PrimaryDatabase = ConfigurationSettings.AppSettings["PrimaryDatabase"];

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

Share

About the Author

cgreen69
Software Developer (Senior)
United Kingdom United Kingdom
A .net devotee specialising in Object Orientated web development for financial institutions in Europe. When not working can normally be found at a bar within walking distance of the office.

Comments and Discussions

 
QuestionGreat utility! Pinmembersearch0010-Jan-12 8:35 
GeneralExactly the type of think I was searching for ... Pinmemberyordan_georgiev24-Jun-09 1:12 
GeneralThis was helpful to me, I had a custom web.config and I was able to extend this class easily to pull various things, helped a bunch. Thanks! PinmemberjollyGreenGiant_0211-Feb-09 10:34 
GeneralSelectSingleNode does not work PinmemberVOJTADOHNAL27-Oct-06 8:06 
AnswerRe: SelectSingleNode does not work PinmemberXymie1-Dec-08 2:06 
Same problem here. This is because the configuration node has a namespace. I fixed it with the addition of a namespace manager. Just change the bold part with your own namespace and it will work.
 
private string SearchXmlForKey(string SearchKey)
{
string AttributeValue = "";
 
string XPath = @"/n:configuration/n:appSettings/n:add[@key='" + SearchKey + "']";
 
XmlNamespaceManager xnm = new XmlNamespaceManager(xmldoc.NameTable);
xnm.AddNamespace("n", "http://schemas.microsoft.com/.NetConfiguration/v2.0");
XmlNode node = node = xmldoc.SelectSingleNode(XPath, xnm);
 
if (node != null && node.Attributes["value"] != null)
{
AttributeValue = node.Attributes["value"].Value;
}
else
{
throw new Exception("Cannot find the " + SearchKey + " key");
}
 
return AttributeValue;
 
}
GeneralUpdate: For Multiple Reads PinmemberXtreemZ26-Jan-06 11:40 
GeneralCode Doesn't Work PinmemberOzzie6-Apr-05 5:51 
GeneralRe: Code Doesn't Work PinmemberPiercesare6-Jun-05 1:16 
QuestionThe same? PinsitebuilderUwe Keim1-Jul-04 18:51 
AnswerRe: The same? Pinmembercgreen691-Jul-04 22:16 
GeneralRe: The same? PinsitebuilderUwe Keim1-Jul-04 22:18 
GeneralRe: The same? Pinmembercgreen691-Jul-04 23:54 
GeneralRe: The same? PinmemberTim Musschoot6-Jul-04 3:09 
GeneralRe: The same? Pinmembercodospra17-Oct-05 3:17 
GeneralRe: The same? PinmemberDave Bacher6-Mar-06 12:25 
AnswerRe: The same? PinadminChris Maunder4-Jul-04 6:45 
GeneralRe: The same? PinsitebuilderUwe Keim4-Jul-04 17:13 
GeneralRe: The same? Pinmembercgreen694-Jul-04 22:10 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web01 | 2.8.141029.1 | Last Updated 1 Jul 2004
Article Copyright 2004 by cgreen69
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid