473,546 Members | 2,249 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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" ?>
<configuratio n>
<appSettings>
<add key="MyKey" value="MyValue" />
</appSettings>
<!-- other stuff here -->
</configuration>
What I want to be able to do is to programmaticall y change the entry in
<appSettings>.. .
FROM
<add key="MyKey" value="MyValue" />
TO
<add key="MyKey" value="YourValu e" />
Thanks!
Nov 17 '05 #1
3 13386
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******** ******@TK2MSFTN GP12.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" ?>
<configuratio n>
<appSettings>
<add key="MyKey" value="MyValue" />
</appSettings>
<!-- other stuff here -->
</configuration>
What I want to be able to do is to programmaticall y change the entry in
<appSettings>.. .
FROM
<add key="MyKey" value="MyValue" />
TO
<add key="MyKey" value="YourValu e" />
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******** ******@TK2MSFTN GP12.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" ?>
<configuratio n>
<appSettings>
<add key="MyKey" value="MyValue" />
</appSettings>
<!-- other stuff here -->
</configuration>
What I want to be able to do is to programmaticall y change the entry in
<appSettings>.. .
FROM
<add key="MyKey" value="MyValue" />
TO
<add key="MyKey" value="YourValu e" />
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" ?>
: <configuratio n>
: <appSettings>
: <add key="MyKey" value="MyValue" />
: </appSettings>
: <!-- other stuff here -->
: </configuration>
:
: What I want to be able to do is to programmaticall y change the entry
: in <appSettings>.. .
: FROM
: <add key="MyKey" value="MyValue" />
: TO
: <add key="MyKey" value="YourValu e" />

Here's one way to do it:

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

XmlNode node = doc.SelectSingl eNode("/configuration/appSettings/add");
if (node is XmlElement)
{
XmlElement add = (XmlElement) node;
add.SetAttribut e("value", "Your Value");
}

XmlTextWriter w = new XmlTextWriter(C onsole.Out);
w.Formatting = Formatting.Inde nted;
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
2917
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 the name of this is set in machine.config. I was hoping it would be possible to change this in web.config. I got it to work, kind of. Good news is I...
1
1906
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 ConfigurationManage.AppSettings when i getting property value. but i also want to set this property.
5
1562
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 of a web app and decided to try it with a regular app). What I discovered was that for some reason the .config file is not being copied with...
7
1576
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 web.config file. I can't seem to work out how to do this. I guess I need to use the AppSettingsSection class, but I'm not sure. I can't find any...
2
2216
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 itdoesn't affect the Application at all! It still uses the old value. Are the Settings.Setting hardcoded somewhere in the application? I can't find...
4
2200
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 time takes, sometimes, three or four of minutes in a medium-level machine (a PIII 1.5 Ghz), when the binary contents are changed, or if the time of...
5
1880
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 that the web.cofig file is different then a normal web app web.config. Do I need to add anything to the web.config file of my existing web app or no?...
1
6045
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 during the runtime, the user can click on a button and change the connection string without exiting the application. I would really appreciate...
4
25976
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
7435
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7694
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7947
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7461
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7792
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
5080
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3470
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1046
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
747
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.