Add Method

ConfigurationSectionCollection.Add Method (String, ConfigurationSection)

 
Namespace:   System.Configuration
Assembly:  System.Configuration (in System.Configuration.dll)

public void Add(
	string name,
	ConfigurationSection section
)

Parameters

name
Type: System.String

The name of the section to be added.

section
Type: System.Configuration.ConfigurationSection

The section to be added.

By default, this method adds the specified ConfigurationSection object if it is not already contained within the collection.

The following example shows how to use the Add method.

static void AddSection()
{

    try
    {
        System.Configuration.Configuration config =
        ConfigurationManager.OpenExeConfiguration(
        ConfigurationUserLevel.None);

        CustomSection customSection =
            new CustomSection();


        string index =
            config.Sections.Count.ToString();

        customSection.FileName =
            "newFile" + index + ".txt";

        string sectionName = "CustomSection" + index;

        TimeSpan ts = new TimeSpan(0, 15, 0);
        customSection.MaxIdleTime = ts;
        customSection.MaxUsers = 100;

        config.Sections.Add(sectionName, customSection);
        customSection.SectionInformation.ForceSave = true;
        config.Save(ConfigurationSaveMode.Full);
    }
    catch (ConfigurationErrorsException err)
    {
        Console.WriteLine(err.ToString());
    }
}

.NET Framework
Available since 2.0
Return to top
Show:
© 2016 Microsoft