Members

Technology Zones

Articles

Hosted By

MaximumASP

Info

Rated
Read 13,332 times

Contents

Related Categories

Edit and Encrypt Web.Config sections using C# 2.0 - Modifying a Web.Config file

Modifying a Web.Config file

  1. Open Web.Config for editing using WebConfigurationManager class.
  2. Using respective Configuration class, bring about the necessary changes.
  3. Save changes to the physical file using Configuration class.
private void UpdateConfig(string strKey, string strValue)
{
Configuration objConfig = WebConfigurationManager.OpenWebConfiguration("~");
AppSettingsSection objAppsettings = (AppSettingsSection)objConfig.GetSection("appSettings");
if (objAppsettings != null)
{
objAppsettings.Settings[strKey].Value = strValue;
objConfig.Save();
}
}

In the above piece of code, OpenWebConfiguration() method of WebConfigurationManager class opens Web.Config file in the root directory and returns it as a Configuration object. GetSection() method of Configuration class accepts path to a specific section as argument. The path is the relative path from the root node "configuration". You can refer to deeper nodes(sections in our context) by their names separated by '/'. For example, to get access to the "authentication" section, provide "system.web/authentication" as the parameter to GetSection() method. It returns a generic ConfigurationSecton object, which can be typecasted to proper configuration section class. In our example we get hold of the "appSettings" section with the help of AppSettingsSection class. AppSettingsSection class instance has a Settings collection property which contains application setting from the configuration section as key-value pairs. The Settings property can be indexed using key to get the corresponding value. You can also set the value property and call the Save() method of the Configuration object to write configurations in the Configuration instance to config file.

To delete an entry in the Web.config file: The Remove() method of Settings collection deletes an entry from the Configuration instance. Remove() method accepts key of the entry to be deleted.

Note: Please do not forget to call the Save() method of the Configuration instance to get the chnages reflected in the physical file.

objAppsettings.Settings.Remove("Location");
To iterate through all the key-value pairs in a configuration section, access the string array of keys via AllKeys property of Settings collection.
foreach (string strKey in objAppsettings.Settings.AllKeys)
{
DataRow dr = dt.NewRow();
dr["Key"] = strKey;
dr["Value"] = objConfig.AppSettings.Settings[strKey].Value;
dt.Rows.Add(dr);
}

Mohammed Habeeb works as a software developer for an India based CMMI L5 Business Solutions Provider InApp Information Technologies situated at technopark. He holds a bachelors in Computer Science Engineering from MES College, Calicut University. He is also a Microsoft Certified Application Developer (MCAD) in .NET Framework. He has a strong inclination towards Microsoft technologies especially the .NET Platform. He is an active member of Microsoft user group, Cochin since 2004. He has a strong passion for science and technology. His interests span through travelling, driving, photography, stamps and coin collection.

Comments