Access Web.Config from a Console / Windows application

C

christopher green

I have a solution that comprises a web based project and a console
application. Web.Config holds attributes that I want to access from my
console application, can anyone advise me as to how to do this?

Restructuring the project to put the values into a database is not an
option.

Is there someway of invoking a web context to allow access to web.config
or should I use a FileStream and read web.config into an xml doc and
parse? If so does any one have any examples of the xml?

Many thanks.





*** Sent via Devdex http://www.devdex.com ***
Don't just participate in USENET...get rewarded for it!
 
I

Ignacio Machin \( .NET/ C# MVP \)

Hi Chris,

Reading and parsing the XML file seems like the best idea. You can use the
code below to do it, it's taken from a CF app but it does what you want.

XmlDocument xSettings = new XmlDocument();

xSettings.Load(new System.IO.StreamReader(configPath));

foreach(XmlNode node in xSettings["configuration"]["appSettings"])

{

switch(node.Attributes["key"].Value)

{

case "RASEntryName":

node.Attributes["value"].Value = entryName;

break;

case "RASPhoneNumber":

node.Attributes["value"].Value = phoneNumber;

break;

case "RASUsername":

node.Attributes["value"].Value = userName;

break;

case "RASPassword":

node.Attributes["value"].Value = password;

break;


Cheers,
 
I

Ignacio Machin \( .NET/ C# MVP \)

Hi,

I don';t think that using this the config of the web application can be
accessed from the console application. You use that construct to access your
own config values, not those of another applucation

Cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

bhargav said:
System.Configuration.ConfigurationSettings.AppSettings["conn_string"].ToStri
ng()
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top