Solved

Get A String Value Of Web.Config File

Posted on 2007-11-16
11
3,032 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
Comment Utility
in asp.net 2.0

use this

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

0
 
LVL 12

Expert Comment

by:needo_jee
Comment Utility
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
Comment Utility
I don't need the connection string value. I need the ENTIRE web.config value.
0
 
LVL 12

Expert Comment

by:needo_jee
Comment Utility
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
Comment Utility
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
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 
LVL 10

Expert Comment

by:joex911
Comment Utility
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
Comment Utility
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
Comment Utility
>>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
Comment Utility
samtran0331, thanks for your repsonse. Exactly what I was after!
0
 

Expert Comment

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

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
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…
This video discusses moving either the default database or any database to a new volume.
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

762 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now