Solved

Get A String Value Of Web.Config File

Posted on 2007-11-16
11
3,034 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
Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

 
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:joex911
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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
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…

810 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