Click here to Skip to main content
15,892,072 members
Articles / Web Development / ASP.NET
Article

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

Rate me:
Please Sign up or sign in to vote.
4.04/5 (8 votes)
30 Jun 2004 113.4K   820   23   18
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

C#
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


Written By
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! Pin
search0010-Jan-12 8:35
search0010-Jan-12 8:35 
GeneralExactly the type of think I was searching for ... Pin
yordan_georgiev24-Jun-09 1:12
yordan_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! Pin
jollyGreenGiant_0211-Feb-09 10:34
jollyGreenGiant_0211-Feb-09 10:34 
GeneralSelectSingleNode does not work Pin
VOJTADOHNAL27-Oct-06 8:06
VOJTADOHNAL27-Oct-06 8:06 
AnswerRe: SelectSingleNode does not work Pin
Xymie1-Dec-08 2:06
Xymie1-Dec-08 2:06 
GeneralUpdate: For Multiple Reads Pin
XtreemZ26-Jan-06 11:40
XtreemZ26-Jan-06 11:40 
GeneralCode Doesn't Work Pin
Ozzie6-Apr-05 5:51
Ozzie6-Apr-05 5:51 
GeneralRe: Code Doesn't Work Pin
Piercesare6-Jun-05 1:16
Piercesare6-Jun-05 1:16 
QuestionThe same? Pin
Uwe Keim1-Jul-04 18:51
sitebuilderUwe Keim1-Jul-04 18:51 
AnswerRe: The same? Pin
cgreen691-Jul-04 22:16
cgreen691-Jul-04 22:16 
GeneralRe: The same? Pin
Uwe Keim1-Jul-04 22:18
sitebuilderUwe Keim1-Jul-04 22:18 
GeneralRe: The same? Pin
cgreen691-Jul-04 23:54
cgreen691-Jul-04 23:54 
GeneralRe: The same? Pin
Tim Musschoot6-Jul-04 3:09
Tim Musschoot6-Jul-04 3:09 
GeneralRe: The same? Pin
codospra17-Oct-05 3:17
codospra17-Oct-05 3:17 
GeneralRe: The same? Pin
Dave Bacher6-Mar-06 12:25
Dave Bacher6-Mar-06 12:25 
cgreen69 wrote:
If you have ever worked in a test driven development environment that utilises automated testing there is often a need to access properties from Web.Config in console / windows apps. Now as you point out "myapp.exe.config" all that is built in for you, BUT I don't want to replicate settings from Web.Config in myapp.exe.config, i want a single config file that holds all the settings required.


I agree with your "keep it professional," and am not the original poster on this thread. However, the Framework can reference external XML files from the appSettings system, which can accomplish what you are doing here (IE sharing configuration between a console, service and WebApp).

To reference an external file from your application settings section, you make your config file look like this:
<configuration>
<system.web>
<compilation defaultLanguage="c#" debug="true" />
</system.web>

<appSettings file="c:\Program Files\MyCommonSettings\Common.config"/>
</configuration>

This tells the framework to load the appsettings from an external configuration file. The file then looks something like this:
<appSettings>
<add key="ConnectionInfo" value="server=(local);database=Northwind;Integrated Security=SSPI" />
</appSettings>

Note that all that this has done is moved the appSettings section to an external file. You can reference that file from as many other config files as you please, file system permissions permitting.

There are times when you can't use System.Configuration, however, for other reasons. For example, you might be a server control running in the IDE and needing to get at web.config in order to read a URL from the file (we've had this situation come up a few times now). What we've typically done is appKey*defaultValue, but that is ugly.

With this class, it is possible to request the project path from the IDE and to use that to find Web.config, and then process it, letting the server control have access to the values in design mode.
AnswerRe: The same? Pin
Chris Maunder4-Jul-04 6:45
cofounderChris Maunder4-Jul-04 6:45 
GeneralRe: The same? Pin
Uwe Keim4-Jul-04 17:13
sitebuilderUwe Keim4-Jul-04 17:13 
GeneralRe: The same? Pin
cgreen694-Jul-04 22:10
cgreen694-Jul-04 22:10 

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

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