469,898 Members | 1,799 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,898 developers. It's quick & easy.

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 1452
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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Frankie | last post: by
4 posts views Thread by Shapper | last post: by
1 post views Thread by somequestion | last post: by
4 posts views Thread by Ravi Ambros Wallau | last post: by
4 posts views Thread by olduncleamos | last post: by
reply views Thread by Michael Bray | last post: by
1 post views Thread by Waqarahmed | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.