473,396 Members | 1,815 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Change value in existing Web.config file

I'm writing a small utility app (C# Windows forms) used to create new
ASP.NET Web sites. This utility needs to be able to change existing values
in an existing Web.config. Please note: I do NOT want to make this change
from any currently running Web application. Rather, I plan to have a
"base/standard Web.config" that is copied then updated by the utility app
for each new Web site.

I'm hoping there is some straight-forward way to do this. I have Googled
this and found lots of overly-complicated stuff (like rewriting the entire
file, doing stuff with XSLT,etc). I'm relatively new to working with XML
files, and that other stuff just seems overkill.

The following is an abbreviated version of the Web.config file:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key="MyKey" value="MyValue" />
</appSettings>
<!-- other stuff here -->
</configuration>
What I want to be able to do is to programmatically change the entry in
<appSettings>...
FROM
<add key="MyKey" value="MyValue" />
TO
<add key="MyKey" value="YourValue" />
Thanks!
Nov 17 '05 #1
3 13378
Replace the values in your base web.config file with tokens (e.g.
[SOME_VALUE]).

From your utility, read the file into a string. Replace the tokens with
whatever values you need, and write it back out again.

"Frankie" <A@B.COM> wrote in message
news:ut**************@TK2MSFTNGP12.phx.gbl...
I'm writing a small utility app (C# Windows forms) used to create new
ASP.NET Web sites. This utility needs to be able to change existing values
in an existing Web.config. Please note: I do NOT want to make this change
from any currently running Web application. Rather, I plan to have a
"base/standard Web.config" that is copied then updated by the utility app
for each new Web site.

I'm hoping there is some straight-forward way to do this. I have Googled
this and found lots of overly-complicated stuff (like rewriting the entire
file, doing stuff with XSLT,etc). I'm relatively new to working with XML
files, and that other stuff just seems overkill.

The following is an abbreviated version of the Web.config file:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key="MyKey" value="MyValue" />
</appSettings>
<!-- other stuff here -->
</configuration>
What I want to be able to do is to programmatically change the entry in
<appSettings>...
FROM
<add key="MyKey" value="MyValue" />
TO
<add key="MyKey" value="YourValue" />
Thanks!

Nov 17 '05 #2
Hi,

What about reading the file, look for the line with the key that you want
and replace the value with your new value.
IMO I think it would be better if you treat it as XML. It's not so complex
at all.

if not try to use Regex, cause you may want to change the vale for the same
key more than once.
cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
"Frankie" <A@B.COM> wrote in message
news:ut**************@TK2MSFTNGP12.phx.gbl...
I'm writing a small utility app (C# Windows forms) used to create new
ASP.NET Web sites. This utility needs to be able to change existing values
in an existing Web.config. Please note: I do NOT want to make this change
from any currently running Web application. Rather, I plan to have a
"base/standard Web.config" that is copied then updated by the utility app
for each new Web site.

I'm hoping there is some straight-forward way to do this. I have Googled
this and found lots of overly-complicated stuff (like rewriting the entire
file, doing stuff with XSLT,etc). I'm relatively new to working with XML
files, and that other stuff just seems overkill.

The following is an abbreviated version of the Web.config file:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key="MyKey" value="MyValue" />
</appSettings>
<!-- other stuff here -->
</configuration>
What I want to be able to do is to programmatically change the entry in
<appSettings>...
FROM
<add key="MyKey" value="MyValue" />
TO
<add key="MyKey" value="YourValue" />
Thanks!

Nov 17 '05 #3
In article <ut**************@TK2MSFTNGP12.phx.gbl>,
Frankie <A@B.COM> wrote:

: [...]
: The following is an abbreviated version of the Web.config file:
:
: <?xml version="1.0" encoding="utf-8" ?>
: <configuration>
: <appSettings>
: <add key="MyKey" value="MyValue" />
: </appSettings>
: <!-- other stuff here -->
: </configuration>
:
: What I want to be able to do is to programmatically change the entry
: in <appSettings>...
: FROM
: <add key="MyKey" value="MyValue" />
: TO
: <add key="MyKey" value="YourValue" />

Here's one way to do it:

static void Main(string[] args)
{
XmlDocument doc = new XmlDocument();
doc.Load("Web.config");

XmlNode node = doc.SelectSingleNode("/configuration/appSettings/add");
if (node is XmlElement)
{
XmlElement add = (XmlElement) node;
add.SetAttribute("value", "Your Value");
}

XmlTextWriter w = new XmlTextWriter(Console.Out);
w.Formatting = Formatting.Indented;
w.Indentation = 3;

doc.WriteTo(w);
}

Hope this helps,
Greg
Nov 17 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: who be dat? | last post by:
Hello all. I'm writing an application that is writing trace information that can be viewed in trace.axd. I would like to rename this and use a different name specific to my application. I know...
1
by: somequestion | last post by:
i work on asp.net 2.0 so i have to change property of web.config file on the web page i mean when i make a board i want to change PageSize in the web.config file. so i use...
5
by: stand__sure | last post by:
I had occasion tonight to write an installer class that changed something in a config file tonight (I had never had a need to do it, but happened upon a "How To" article that explained it in terms...
7
by: Alan Silver | last post by:
Hello, I have a web site that uses themes. I would like to have an option on the site owner's administration page where they can set the theme. This page would then write the new value into the...
2
by: Pieter | last post by:
Hi, I'm using the Settings.settings of VB.NET to define a Connectionstring (Scope = application). When I deploy the Solution, and change this Setting in the app.config-file, it seems that...
4
by: Ravi Ambros Wallau | last post by:
Hi: We developed a set of ASP.NET Web Applications that never runs in stand-alone mode, but always inside a portal (Rainbow Portal). All modules are copied on that portal. My question is: load...
5
by: igotyourdotnet | last post by:
I have a question on atlas: I can create an 'atlas project' and I can add atlas controls fine. but when I try and add an atlas control to an existing asp.net web app I can't, Why? I did notice...
1
by: Sankalp | last post by:
Hi, I am using VB 2005. My application has many data bound controls. The connection is stored in the app.config file. I want the application to start with a default connection string and while...
4
by: olduncleamos | last post by:
I am trying to share one config file between multiple console apps. Is there anyway I can set the config file name for the current application? Thanks in advance.
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.