IT Community - Software Programming, Web Development and Technical Support

How to Edit the web.config file elements using c#?

This is a discussion on How to Edit the web.config file elements using c#? within the C# Programming forums, part of the Software Development category; How to Edit the web.config file elements using c#? ASP.NET 1.x allowed configurations in web.config file ...


Go Back   IT Community - Software Programming, Web Development and Technical Support > Software Development > C# Programming

Register FAQ Members List Calendar Mark Forums Read
  #1  
Old 07-17-2007, 09:55 PM
Archer Archer is offline
D-Web Programmer
 
Join Date: Jun 2007
Posts: 52
Archer is on a distinguished road
Default How to Edit the web.config file elements using c#?

How to Edit the web.config file elements using c#?

ASP.NET 1.x allowed configurations in web.config file to be read from a .NET application. But, there were no options to manipulate Web.Config contents programatically. To achieve this, you had to consider the Web.Config file as a normal file or an XML file. .NET 2.0 fills this gap and also provides many other useful operations to be carried out on a Web.Config file, such as editing and encrypting sections of the Web.Config file

The classes and methods to take control of the Web.Config file span across two namespaces:
• System.Configuration
• System.Web.Configuration
Each section in the Web.Config file has a corresponding class in either of the namespaces. These classes allow modification of the corresponding sections. The classes for sections within the "system.web" section are found in System.Web.Configuration. Classes for other sections that are not specific to Web.Config are found in System.Configuration.
Modifying a section in Web.Config
1. Open Web.Config for editing by using the WebConfigurationManager class.
2. Use the respective Configuration class to make the necessary changes.
3. Save the changes to the physical file by using the Configuration class.
private void UpdateConfig(string strKey, string strValue)
{
Configuration objConfig =
WebConfigurationManager.OpenWebConfiguration("~");
AppSettingsSection objAppsettings =
(AppSettingsSection)objConfig.GetSection("appSetti ngs");
if (objAppsettings != null)
{
objAppsettings.Settings[strKey].Value = strValue;
objConfig.Save();
}
}
In the above piece of code, the OpenWebConfiguration() method of the WebConfigurationManager class opens the Web.Config file in the root directory and returns it as a Configuration object. The GetSection() method of the Configuration class accepts the path to a specific section as an argument. The path is the relative path from the root node "configuration". You can refer to deeper nodes (sections, in this context) by their names separated by '/'. For example, to get access to the "authentication" section, provide "system.web/authentication" as the parameter to the GetSection() method. It returns a generic ConfigurationSecton object that can be typecast to the proper configuration section class. In this example, you get hold of the "appSettings" section with the help of the AppSettingsSection class. The AppSettingsSection class instance has a Settings collection property that contains the application setting from the configuration section as key-value pairs. The Settings property can be indexed using key to get the corresponding value. You also can set the value property and call the Save() method of the Configuration object to write configurations in the Configuration instance to the config file.
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Config File or Registry shaalini Server Management 4 06-14-2009 08:41 PM
Web.config nhoj ASP and ASP.NET Programming 8 12-10-2007 10:07 PM
Change the Asp.net (C#) Web.config file a.deeban ASP and ASP.NET Programming 1 08-18-2007 03:42 AM
What is Machine.config? H2o ASP and ASP.NET Programming 1 07-24-2007 02:18 AM
How to encrypt app.config file in .net.? bluesky C# Programming 0 07-17-2007 09:59 PM


All times are GMT -7. The time now is 05:38 PM.


Copyright ©2004 - 2007, DiscussWeb. All Rights Reserved.
Our Partners
One Way Moving Companies | Stamford Dentist | Euro Millions Lottery | Home Loans| Furniture

SEO by vBSEO 3.0.0