Solved

Get A String Value Of Web.Config File

Posted on 2007-11-16
11
3,035 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
11 Comments
 
LVL 12

Expert Comment

by:needo_jee
ID: 20299340
in asp.net 2.0

use this

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

0
 
LVL 12

Expert Comment

by:needo_jee
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
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 12

Expert Comment

by:needo_jee
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:needo_jee
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 24

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 500 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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Winforms -- Capture CTRL+SHIFT+D at the form not working? 3 26
MailAddress in vb 4 29
Need syntax help building hyperlink 2 27
Problem to Office 1 15
User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

830 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