Modifying Web.Config file in Runtime

You can modify the Web.config file in runtime using the following simple code.

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

// Get the configuration file.
Configuration oConfig = WebConfigurationManager.OpenWebConfiguration("~");
// Change the value of the key
oConfig.AppSettings.Settings["Key"].Value = "New value" ;
// save the changes back to the file
oConfig.Save();

You can get any section of the Web.Config using GetSection Method.
For Example,

ConfigurationSection section= oConfig .GetSection("connectionStrings");

returns the connectionstrings Section.

Gopikrishna

    Blogger Comment
    Facebook Comment

0 comments:

Post a Comment