You can use WebConfigurationManager class defined in System.Web.Configuration namespace to retrieve values from a web.config file if you are working on a web application or System.Configuration.ConfigurationManager class in case you are working on a windows application. e.g. If your web.config has a this value
Then these can be read in manner below string test = ConfigurationManager.AppSettings["test"]; string con1 = ConfigurationManager.ConnectionStrings["con1"];
To edit the web.config file protected void EditConfigButton(object sender, EventArgs e) { Configuration objConfig = System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration("~"); AppSettingsSection objAppsettings = (AppSettingsSection)objConfig.GetSection("appSettings"); //Edit if (objAppsettings != null) { objAppsettings.Settings["test"].Value = "newvalueFromCode"; objConfig.Save(); } } protected void EncryptConfigButton(object sender, EventArgs e) {
//Encrypt Configuration objConfig2 = System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration(Request.ApplicationPath); AppSettingsSection objAppsettings2 = (AppSettingsSection)objConfig.GetSection("appSettings"); if (!objAppsettings2.SectionInformation.IsProtected) { objAppsettings2.SectionInformation.ProtectSection("RsaProtectedConfigurationProvider"); objAppsettings2.SectionInformation.ForceSave = true; objConfig2.Save(ConfigurationSaveMode.Modified); } } protected void CreateConfigSectionButton(object sender, EventArgs e) { //Create Configuration objConfig = System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration("~"); AppSettingsSection objAppsettings = (AppSettingsSection)objConfig.GetSection("appSettings");
objAppsettings.Settings.Add("newKey","newValue"); objConfig.Save(ConfigurationSaveMode.Modified); } protected void DecryptConfig_Click(object sender, EventArgs e) { Configuration objConfig = System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration(Request.ApplicationPath); AppSettingsSection objAppsettings = (AppSettingsSection)objConfig.GetSection("appSettings"); if (objAppsettings.SectionInformation.IsProtected) { objAppsettings.SectionInformation.UnprotectSection(); objAppsettings.SectionInformation.ForceSave = true; objConfig.Save(ConfigurationSaveMode.Modified); }
}
//To Remove protected void RemoveSectionButton_Click(object sender, EventArgs e) { Configuration objConfig = System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration(Request.ApplicationPath); AppSettingsSection objAppsettings = (AppSettingsSection)objConfig.GetSection("appSettings"); objAppsettings.Settings.Remove("Location"); }
The important thing to note is that you can get any section of web.config in your code by using AppSettingsSection objAppsettings = (AppSettingsSection)objConfig.GetSection("appSettings"); Just replace appSettings with the section name you want to play around with.
|
| Author: Himanshu Sharma 28 May 2008 | Member Level: Bronze Points : 2 |
This is what i am looking for... Thanks a lot sir ,it is really helpful for me.We have got lot of thing on these posted article.
|
| Author: Rakesh Kumar 06 Jun 2008 | Member Level: Bronze Points : 0 |
Excellent Article Thanx sir
|
| Author: Mahesh Raj 07 Jun 2008 | Member Level: Gold Points : 1 |
This is very good information,Continue posting such useful articles.
|