473,724 Members | 2,240 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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
AppSettingsSect ion 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 AppSettingsSect ion 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 1587
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
AppSettingsSec tion 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 AppSettingsSect ion 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*********@no spam.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
AppSettingsSec tion 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 AppSettingsSect ion 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
13395
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 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...
4
1996
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 variable. <img src="images/ Session("culture") Value /myImage.jpg" /> This is what I did: <img src='images/<%# Session("culture") %>/myImage.jpg' />
1
1914
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
1579
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 Primary Output. In checking a few other apps for which I had written installers I found that they too...
2
2224
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 any documentation of this subject, or anyway how I should do
4
2208
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 last modification of the web.config file is changed. An application that runs in "stand-alone" mode...
10
6099
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 this snip of code. I am trying to set the radio button with this link of code: echo 'SCRIPT language=JavaScript setCheckedValue("'.$_SESSION.'");</SCRIPT>'; //? <snip of code>
4
26051
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
1053
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 application that stores it's configuration in a file in the Application directory. If the application is called "MyApp.exe", the configuration file is "MyApp.config". Note that this is not the same as "MyApp.exe.config" which is where the .NET-based XML-format configuration normally goes. This...
0
8868
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8741
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 synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9388
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. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9241
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 captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8062
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6685
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5996
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 into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
3209
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2149
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 effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.