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 ...
| |||||||
| Register | FAQ | Members List | Calendar | Mark Forums Read |
| |||
| 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. |
| Sponsored Links |
![]() |
| Thread Tools | |
| Display Modes | |
| |
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Web.config | nhoj | ASP and ASP.NET Programming | 8 | 12-10-2007 11:07 PM |
| Change the Asp.net (C#) Web.config file | a.deeban | ASP and ASP.NET Programming | 1 | 08-18-2007 04:42 AM |
| Ruby on Rails server config | vivekanandan | Ruby | 7 | 08-04-2007 05:02 AM |
| What is Machine.config? | H2o | ASP and ASP.NET Programming | 1 | 07-24-2007 03:18 AM |
| How to encrypt app.config file in .net.? | bluesky | C# Programming | 0 | 07-17-2007 10:59 PM |