C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Communities   Interview   Jobs   Projects   Offshore Development    
Silverlight Tutorials | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Revenue Sharing |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...

New Feature: Community Sites: Create your own .NET community website and start earning from Google AdSense ! It's Free !






Edit ,Delete , Create and Encrypt sections in web.config file


Posted Date: 27 May 2008    Resource Type: Articles    Category: Web Applications

Posted By: shakti singh tanwar       Member Level: Diamond
Rating:     Points: 15



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.






Responses

Author: Himanshu Sharma    28 May 2008Member 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 2008Member Level: Bronze   Points : 0
Excellent Article
Thanx sir


Author: Mahesh Raj    07 Jun 2008Member Level: Gold   Points : 1
This is very good information,Continue posting such useful articles.


Feedbacks      
Popular Tags   What are tags ?   Search Tags  
Web Service Session  .  

Post Feedback


This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
You must Sign In to post a response.
Next Resource: Site Navigation using SiteMapPath control in ASP.Net 2.0
Previous Resource: Encrypting Web.config in ASP.NET 2.0 Applications:
Return to Discussion Resource Index
Post New Resource
Category: Web Applications


Post resources and earn money!
 
Related Resources



dotNet Slackers   BizTalk Adaptors    Web Design

Help Desk

Contact Us    Privacy Policy    Terms Of Use