views:

3196

answers:

6

Hi, I am confused on how to modify the web.config appSettings values at runtime. For example, I have this appSettings section:

<appSettings>
  <add key="productspagedesc" value="TODO: Edit this default message" />
  <add key="servicespagedesc" value="TODO: Edit this default message" />
  <add key="contactspagedesc" value="TODO: Edit this default message" />
  <add key="aboutpagedesc" value="TODO: Edit this default message" />
  <add key="homepagedesc" value="TODO: Edit this default message" />
 </appSettings>

Let's say, I want to modify the "homepagedesc" key at runtime. I tried ConfigurationManager and WebConfigurationManager static classes but the settings are "read-only". Can someone please teach me how to modify appSettings values at runtime? Thanks in advance!

A: 

Changing the web.config generally causes an application restart.

If you really need your application to edit its own settings, then you should consider a different approach such as databasing the settings or creating an xml file with the editable settings.

Joel Potter
Hi, thanks for the response. But there is this "Configuration" class that has a "Save" function. Do you really have to restart the app for the new settings to be active?
jerbersoft
+4  A: 

You need to use WebConfigurationManager.OpenWebConfiguration(): For Example:

Dim myConfiguration As Configuration = System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration("~")
        myConfiguration.ConnectionStrings.ConnectionStrings("myDatabaseName").ConnectionString = txtConnectionString.Text
myConfiguration.AppSettings.Settings.Item("myKey").Value = txtmyKey.Text
myConfiguration.Save()

I think you might also need to set AllowLocation in machine.config. This is a boolean value that indicates whether individual pages can be configured using the element. If the "allowLocation" is false, it cannot be configured in individual elements.

Finally, it makes a difference if you run your application in IIS and run your test sample from Visual Studio. The ASP.NET process identity is the IIS account, ASPNET or NETWORK SERVICES (depending on IIS version).

Might need to grant ASPNET or NETWORK SERVICES Modify access on the folder where web.config resides.

Mitch Wheat
Thanks for the response Mitch. You answered my question. What I did was I run VS 2008 as Administrator and everything was doing fine.
jerbersoft
+1  A: 

Whatever you do with this space, please be advised that the work in Vista is a bit different to same work on Winxp / 2000 platform as it seems that the user account permissions and setup seems to affect this web config editing enormously in a web environment on IIS. Regards, Andy

Andy
A: 

Hello All:

Here is a good link that nicely explain about modifying the web.config at runtime and its impact in application.

http://aspdotnethacker.blogspot.com/2010/05/modify-webconfig-file-at-runtime.html

Sandy
A: 

good one...

have a look...

http://www.a2zmenu.com/CSharp/Modify%20webconfig%20file%20at%20runtime.aspx

Sandeep
A: 

Try XML Webpad -

It is an excellent light weight framework which not only allows vieweing and editing capabilities of XML files out-of-box but can be easily integrated with any ASP.Net application. And is specially useful to be modify web.config files at runtime.

Udayan