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

A

Alan Silver

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?
 
K

Keith Patrick

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.
 
K

Kevin Steffer [MCP]

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.
 
A

Alan Silver

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.
 
A

Alan Silver

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.
 
A

Alan Silver

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.
 
A

Alan Silver

I have an article on writing / reading configuration files in ASP.NET

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,060
Latest member
BuyKetozenseACV

Latest Threads

Top