Click here to Skip to main content
Licence 
First Posted 30 Jun 2004
Views 78,478
Bookmarked 22 times

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

By | 30 Jun 2004 | Article
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

About the Author

cgreen69

Software Developer (Senior)

United Kingdom United Kingdom

Member

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.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
QuestionGreat utility! Pinmembersearch008:35 10 Jan '12  
GeneralExactly the type of think I was searching for ... Pinmemberyordan_georgiev1:12 24 Jun '09  
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_0210:34 11 Feb '09  
GeneralSelectSingleNode does not work PinmemberVOJTADOHNAL8:06 27 Oct '06  
AnswerRe: SelectSingleNode does not work PinmemberXymie2:06 1 Dec '08  
GeneralUpdate: For Multiple Reads PinmemberXtreemZ11:40 26 Jan '06  
GeneralCode Doesn't Work PinmemberOzzie5:51 6 Apr '05  
GeneralRe: Code Doesn't Work PinmemberPiercesare1:16 6 Jun '05  
QuestionThe same? PinsitebuilderUwe Keim18:51 1 Jul '04  
AnswerRe: The same? Pinmembercgreen6922:16 1 Jul '04  
GeneralRe: The same? PinsitebuilderUwe Keim22:18 1 Jul '04  
GeneralRe: The same? Pinmembercgreen6923:54 1 Jul '04  
GeneralRe: The same? PinmemberTim Musschoot3:09 6 Jul '04  
GeneralRe: The same? Pinmembercodospra3:17 17 Oct '05  
GeneralRe: The same? PinmemberDave Bacher12:25 6 Mar '06  
AnswerRe: The same? PinadminChris Maunder6:45 4 Jul '04  
GeneralRe: The same? PinsitebuilderUwe Keim17:13 4 Jul '04  
GeneralRe: The same? Pinmembercgreen6922:10 4 Jul '04  

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.

Permalink | Advertise | Privacy | Mobile
Web02 | 2.5.120517.1 | Last Updated 1 Jul 2004
Article Copyright 2004 by cgreen69
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid