Welcome to Comunidade Bloggers |create|it| Sign in | Join | Help
Add a custom section to web.config

If you have created a custom section like this, and if you want to add it programmatically to web.config then you have to:

  1. Use the WebConfigurationManager class and open the web

Configuration config = WebConfigurationManager.OpenWebConfiguration(path, site);

From MSDN

path

The virtual path to the configuration file.

site

The name of the application Web site, as displayed in IIS configuration.

 

  1. Create a ConfigurationSectionGroup and add it to the Configuration object

ConfigurationSectionGroup sectionGroup = new ConfigurationSectionGroup();

config.SectionGroups.Add("GROUP_NAME", sectionGroup);

 

  1. Create a new instance of your section (MyCustomSection) and add it to the section group

MyCustomSection section = new MyCustomSection ();

sectionGroup.Sections.Add("SECTION_NAME", section);

 

  1. Fill your custom section properties

section.Property1 = "0123456789";

section.Property2 = "Lorem ipsum dolor sit amet";

 

  1. And save the configuration

config.Save();

If you take a look at your web.config you will see that it was modified programmatically as you specified. I use this technique in the setup process of sites. Cool!

Posted: Tuesday, October 23, 2007 6:40 PM by ricardo.costa
Filed under: ,

Comments

Ricardo Costa : How to create a custom section for an application config file said:

PingBack from http://blogit.create.pt/blogs/ricardocosta/archive/2007/10/16/How-to-create-a-custom-section-for-an-application-config-file.aspx
# October 23, 2007 6:44 PM
Anonymous comments are disabled