Showing posts with label Dynamicly Change Config File. Show all posts
Showing posts with label Dynamicly Change Config File. Show all posts

Monday, August 10, 2009

Dynamically change web.config file

Dynamically change web.config file
==============================

Description

// Suppose you want to remove a handler from 'httpHandlers'
// and you want to remove modules from your 'httpModules'
// section of you web.config file..

// Below, this is suppose your web.confile file's sections

<httpHandlers>
<add verb="GET" type="DJ.Blog.FileUpload.UploadProgressHandler, FileUploadLibrary" path="DJUploadProgress.ashx" />
</httpHandlers>
<httpModules>
<add name="DJUpload" type="DJ.Blog.FileUpload.UploadModule, FileUploadLibrary"/>
</httpModules>

Code

// Now, you want to remove both section from you web.config file.

// Using these Namespaces

using System.Configuration;
using System.Web.Configuration;

// Write this code to your page_load() event

Configuration objConfig = WebConfigurationManager.OpenWebConfiguration(Request.ApplicationPath);
HttpHandlersSection sec = (HttpHandlersSection) objConfig.GetSection("system.web/httpHandlers");
sec.Handlers.Remove("GET", "DJUploadProgress.ashx");

HttpModulesSection sec2 = (HttpModulesSection)objConfig.GetSection("system.web/httpModules");

sec2.Modules.Remove("DJUpload");

objConfig.Save();

// Your web.cofig is now changed dynamically.