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






4.04/5 (8 votes)
Jul 1, 2004

114611

821
Example of how to read web.config settings into a console or Windows application.
Introduction
This code provides an easy to use mechanism for reading A
ppSettings
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"];