472,331 Members | 1,781 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

How do I change a value in web.config from code?

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 concrete
examples of doing this anywhere. I tried copying the sample code from
the MSDN docs for the AppSettingsSection class, but this seemed to be
working on an empty file. I couldn't see how to tell it to look at
web.config.

Anyone able to help?

--
Alan Silver
(anything added below this line is nothing to do with me)
Dec 6 '05 #1
7 1516
I don't know how to set values in the config (outside manually manipulating
the file bits), but I do a similar thing as you: each user can choose a
theme, but the config defines a default. One way around this is to assign a
theme to the anonymous profile (the profile with no UserName) and manipulate
it that way.
Dec 6 '05 #2
I think the web.config is readonly in runtime 'cause the web
application is forces a restart the it detects a changed web.config
file. So either if you can change it I don't think its saved until the
webapp restarts or the web.config is readonly in runtime.
- But I actually haven't tried this one.

Dec 6 '05 #3
>I don't know how to set values in the config (outside manually manipulating
the file bits), but I do a similar thing as you: each user can choose a
theme, but the config defines a default. One way around this is to assign a
theme to the anonymous profile (the profile with no UserName) and manipulate
it that way.


The difference here is that it's the web site owner who chooses the
theme, and *all* users see that theme. They don't get the chance to
change it.

The purpose is to allow the site owner to set seasonal themes, which
saves me having to change the site for them.

I'm not too familiar with profiles, so I'm not sure if this would help.
Isn't the anonymous profile still unique to each anonymous user? I
thought it was a way of non-logged in users being able to have
personalisation. I could be wrong, but if it is like this, then it won't
work for my scenario.

Thanks anyway.

--
Alan Silver
(anything added below this line is nothing to do with me)
Dec 6 '05 #4
>I think the web.config is readonly in runtime 'cause the web
application is forces a restart the it detects a changed web.config
file. So either if you can change it I don't think its saved until the
webapp restarts or the web.config is readonly in runtime.
- But I actually haven't tried this one.


AFAICS from the docs, version 2.0 has classes (see AppSettings for
example) designed to allow you to read and write the application config
files, including web.config. All that happens is that the application is
automatically restarted when you change it.

Trouble is, I can't find any concrete examples of changing it. I know I
can do it like any other XML file (which is all web.config is), but I
thought I might as well use the specialised classes as they are there.

--
Alan Silver
(anything added below this line is nothing to do with me)
Dec 6 '05 #5
In case anyone is interested, I found out how to do this. I downloaded
the SDK 2.0 and found it in about 3 clicks!! The ASP.NET section in the
SDK has a section about configuration files, and in there is info about
the configuration API, including a link to a page called "Using the
Configuration Classes" that contains sample code for updating a value in
web.config ;-)

After reading it, I then realised that the admin page that would be
doing this is on a different domain from the site whose web.config file
I want to change, so this isn't going to work for me!! I'll have to read
the file like a normal XML file anyway!! Still, useful to know how it
works.

Hope this is of use to someone.
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 concrete
examples of doing this anywhere. I tried copying the sample code from
the MSDN docs for the AppSettingsSection class, but this seemed to be
working on an empty file. I couldn't see how to tell it to look at
web.config.

Anyone able to help?


--
Alan Silver
(anything added below this line is nothing to do with me)
Dec 7 '05 #6
I have an article on writing / reading configuration files in ASP.NET
2.0:

http://odetocode.com/Articles/418.aspx

--
Scott
http://www.OdeToCode.com/blogs/scott/

On Tue, 6 Dec 2005 18:54:59 +0000, Alan Silver
<al*********@nospam.thanx> wrote:
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 concrete
examples of doing this anywhere. I tried copying the sample code from
the MSDN docs for the AppSettingsSection class, but this seemed to be
working on an empty file. I couldn't see how to tell it to look at
web.config.

Anyone able to help?


Dec 7 '05 #7
>I have an article on writing / reading configuration files in ASP.NET
2.0:

http://odetocode.com/Articles/418.aspx


Thanks for that. As I mentioned in a follow-up post, I realised
afterwards that the web.config file would be on a different domain, so
these APIs won't be any use here. I have had to resort to old-fashioned
XML input and output.

The article's good though. I've bookmarked it for future use. Thanks.

--
Alan Silver
(anything added below this line is nothing to do with me)
Dec 7 '05 #8

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

Similar topics

3
by: Frankie | last post by:
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...
4
by: Shapper | last post by:
Hello, I have this image in my HTML code: <img src="images/en-US/myImage.jpg" /> I want to change the URL using the value of a Session...
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...
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...
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...
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...
10
by: IchBin | last post by:
I am trying to set the state of a radio button. I do not see what I am doing wrong. Sorry, I am new at this.. I need another set of eyes to look at...
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? ...
0
by: Michael Bray | last post by:
I've found a change in SP1 that doesn't seem to make sense to me, but seems to be there nonetheless. The situation is this... I have an...
0
by: tammygombez | last post by:
Hey fellow JavaFX developers, I'm currently working on a project that involves using a ComboBox in JavaFX, and I've run into a bit of an issue....
0
by: tammygombez | last post by:
Hey everyone! I've been researching gaming laptops lately, and I must say, they can get pretty expensive. However, I've come across some great...
0
better678
by: better678 | last post by:
Question: Discuss your understanding of the Java platform. Is the statement "Java is interpreted" correct? Answer: Java is an object-oriented...
0
by: teenabhardwaj | last post by:
How would one discover a valid source for learning news, comfort, and help for engineering designs? Covering through piles of books takes a lot of...
0
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and...
0
by: CD Tom | last post by:
This only shows up in access runtime. When a user select a report from my report menu when they close the report they get a menu I've called Add-ins...
0
jalbright99669
by: jalbright99669 | last post by:
Am having a bit of a time with URL Rewrite. I need to incorporate http to https redirect with a reverse proxy. I have the URL Rewrite rules made...
0
by: Matthew3360 | last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function. Here is my code. ...
0
by: AndyPSV | last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable...

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.