ConfigurationSectionCollection.Add(String, ConfigurationSection) メソッド

定義

ConfigurationSectionCollection オブジェクトに ConfigurationSection オブジェクトを追加します。

public:
 void Add(System::String ^ name, System::Configuration::ConfigurationSection ^ section);
public void Add (string name, System.Configuration.ConfigurationSection section);
member this.Add : string * System.Configuration.ConfigurationSection -> unit
Public Sub Add (name As String, section As ConfigurationSection)

パラメーター

name
String

追加するセクションの名前。

section
ConfigurationSection

追加するセクション。

Add メソッドを使用する方法の例を次に示します。

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());
    }
}
Shared Sub AddSection()

    Try
        Dim config _
        As System.Configuration.Configuration = _
        ConfigurationManager.OpenExeConfiguration( _
        ConfigurationUserLevel.None)

        Dim customSection _
        As New CustomSection()


        Dim index As String = _
        config.Sections.Count.ToString()

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

        Dim sectionName As String = _
        "CustomSection" + index

        Dim ts As 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 err As ConfigurationErrorsException
        Console.WriteLine(err.ToString())
    End Try
End Sub

注釈

既定では、このメソッドは、指定した ConfigurationSection オブジェクトがコレクション内にまだ含まれていない場合に追加します。

適用対象

こちらもご覧ください