Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

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

0.00/5 (No votes)
30 Jun 2004 1  
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