Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Get A String Value Of Web.Config File

Posted on 2007-11-16
11
Medium Priority
?
3,038 Views
Last Modified: 2011-10-03
Experts, I am working on a project where I need to get a string value of my web.config file. Is there a way to do this? I attempted to do:

FileStream fs = File.Open(file, FileMode.Open, FileAccess.Read);
StreamReader sr = new StreamReader(fs, Encoding.ASCII);
Response.Write(sr.ReadToEnd());
sr.Dispose();
fs.Dispose();

But it gives me an empty string.

I also tried copying the file first, web.config.copy, and then reading it. Also , It gave me an empty string.

Any ideas? Do I need to create an XmlDocument?

Thanks for the help.
0
Comment
Question by:clickclickbang
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
11 Comments
 
LVL 12

Expert Comment

by:Munawar Hussain
ID: 20299340
in asp.net 2.0

use this

string connStr = ConfigurationManager.ConnectionString["MyDbConn1"].ToString();

0
 
LVL 12

Expert Comment

by:Munawar Hussain
ID: 20299349
this should be in webcofig
<?xml version="1.0" encoding="utf-8" ?><configuration>   <connectionStrings>      <add name="MyCnString"       connectionString="Data Source=legolas;Initial Catalog=Northwind;Integrated Security=True"       providerName="System.Data.SqlClient"/>
   </connectionStrings>
</configuration>

Add a reference to the System.Configuration.dll and then add a using statement like this :

   using System.Configuration;
The code to grab the connection string could then look like this:


   string cnString = ConfigurationManager.ConnectionStrings["MyCnString"].ConnectionString;   SqlConnection cn = new SqlConnection(cnString);
0
 
LVL 1

Author Comment

by:clickclickbang
ID: 20299377
I don't need the connection string value. I need the ENTIRE web.config value.
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 12

Expert Comment

by:Munawar Hussain
ID: 20299420
this is the complete code sample

here for database installation user has read config file completely and then replaced connectionstring
http://www.aspnetpro.com/NewsletterArticle/2003/12/asp200312bn_l/asp200312bn_l.asp

at the end of article code is given for download.

0
 
LVL 12

Expert Comment

by:Munawar Hussain
ID: 20299441
in the sample go to dir "DBInstallerLib" inside deployment dir

and open the DBinstaller class file

there you see funtion
UpdateConfigConnStr()

thoug i could paste code here but would be good to have a running example

thanks
0
 
LVL 10

Expert Comment

by:Oliver Amaya
ID: 20299700
Hi, could you tell us why you need the entire contents of the web.config file in a string?
0
 
LVL 25

Expert Comment

by:DBAduck - Ben Miller
ID: 20299845
The first thing you need to do is to make sure that you are trying to get the right file.

In a Web Application you should be using MapPath to get the entire filename and directory so that you can open it with a FileStream.

What is in the variable 'file'?
0
 
LVL 37

Accepted Solution

by:
samtran0331 earned 2000 total points
ID: 20300161
>>Do I need to create an XmlDocument?
Yes, that's the easiest and most efficient way...for example, to read the Authentication setting in the web.confg:


    XmlDocument myConfig = new XmlDocument();
    string strPhysicalPathToWebConfig = "somePath";
    myConfig.Load(strPhysicalPathToWebConfig);
    XmlNamespaceManager nsmgr = new XmlNamespaceManager(myConfig.NameTable);
    nsmgr.AddNamespace("web", "http://schemas.microsoft.com/.NetConfiguration/v2.0");
    XmlElement r = myConfig.DocumentElement;
   
    string AuthenticationMode = r.SelectSingleNode("//web:add[@key='Authentication']/@value", nsmgr).Value;
0
 
LVL 1

Author Comment

by:clickclickbang
ID: 20300624
samtran0331, thanks for your repsonse. Exactly what I was after!
0
 

Expert Comment

by:thetadis
ID: 22080987
How can i get the value of a key in the web.config file?
0

Featured Post

[Webinar] Lessons on Recovering from Petya

Skyport is working hard to help customers recover from recent attacks, like the Petya worm. This work has brought to light some important lessons. New malware attacks like this can take down your entire environment. Learn from others mistakes on how to prevent Petya like worms.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…
Suggested Courses

715 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question