Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Get A String Value Of Web.Config File

Posted on 2007-11-16
11
Medium Priority
?
3,040 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
10 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 Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

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…
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
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
Loops Section Overview
Suggested Courses

824 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