How to programmaticaly change the web.config?

P

PSiegmann

Hello group..

Is there a way to change web.config entries at runtime? I haven't
found a class for that.
I have around 20 entries in the web.config, and I want a add a
configuration menu into my app, so that the administrator can change
some of the settings, without editing the web.config directly with a
text editor.
 
R

Riki

Hello group..

Is there a way to change web.config entries at runtime? I haven't
found a class for that.
I have around 20 entries in the web.config, and I want a add a
configuration menu into my app, so that the administrator can change
some of the settings, without editing the web.config directly with a
text editor.

In ASP.NET 2.0, you can use the System.Configuration classes, see
http://www.codeproject.com/KB/dotnet/mysteriesofconfiguration.aspx

You can also write to the web.config file in the same way as you would to
any XML file, something like (in VB.NET):
-----------------------------
Try
' Open web.config file
Dim doc As New XmlDocument()
doc.Load(Server.MapPath("web.config"))

Dim strSel As String
' Use an XPath query to look up the user element in this configuration
having
' a matching "name" attribute
strSel =
"/configuration/system.web/authentication/forms/credentials/user[@name='" &
strUserName & "']"
Dim node As XmlNode = doc.SelectSingleNode(strSel)
' Modify the element
Dim element As XmlElement = CType(node,XmlElement)
element.SetAttribute("password",strHash)

' Save the configuration
doc.Save(Server.MapPath("web.config"))
Catch ex As Exception
Trace.Warn(ex.ToString())
End Try
 
M

Mark Stevens

Is this any use http://odetocode.com/Articles/418.aspx

Regards,
Mark

Hello group..

Is there a way to change web.config entries at runtime? I haven't
found a class for that.
I have around 20 entries in the web.config, and I want a add a
configuration menu into my app, so that the administrator can change
some of the settings, without editing the web.config directly with a
text editor.
--
|\ _,,,---,,_ A picture used to be worth a
ZZZzzz /,`.-'`' -. ;-;;, thousand words - then along
|,4- ) )-,_. ,\ ( `'-' came television!
'---''(_/--' `-'\_)

Mark Stevens (mark at thepcsite fullstop co fullstop uk)
 
E

Eliyahu Goldin

The way to go is to put all your setting that can change in a separate file.

Put this line in the <configuration> section on web.config

<appSettings file="MySettings.xml"/>

Having done that, you can access parameteres in MySettings.xml via
ConfigurationManager.AppSettings.

--
Eliyahu Goldin,
Software Developer
Microsoft MVP [ASP.NET]
http://msmvps.com/blogs/egoldin
http://usableasp.net
 

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

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,813
Latest member
lawrwtwinkle111

Latest Threads

Top