ConfigurationSection Konstruktor

Definition

Initialisiert eine neue Instanz der ConfigurationSection-Klasse.

protected:
 ConfigurationSection();
protected ConfigurationSection ();
Protected Sub New ()

Beispiele

Im folgenden Beispiel wird die Verwendung des ConfigurationSection-Konstruktors veranschaulicht. In diesem Beispiel wird davon ausgegangen, dass Sie eine benutzerdefinierte Abschnittsklasse mit dem Namen CustomSectionerstellt haben. Ein Beispiel für eine solche Klasse finden Sie in der ConfigurationSection Klassenübersicht.


// Create a custom section.
static void CreateSection()
{
    try
    {

        CustomSection customSection;

        // Get the current configuration file.
        System.Configuration.Configuration config =
                ConfigurationManager.OpenExeConfiguration(
                ConfigurationUserLevel.None);

        // Create the section entry  
        // in the <configSections> and the 
        // related target section in <configuration>.
        if (config.Sections["CustomSection"] == null)
        {
            customSection = new CustomSection();
            config.Sections.Add("CustomSection", customSection);
            customSection.SectionInformation.ForceSave = true;
            config.Save(ConfigurationSaveMode.Full);
        }
    }
    catch (ConfigurationErrorsException err)
    {
        Console.WriteLine(err.ToString());
    }
}
' Create a custom section.
Shared Sub CreateSection()
   Try
      
      Dim customSection As CustomSection
      
      ' Get the current configuration file.
         Dim config As System.Configuration.Configuration = _
         ConfigurationManager.OpenExeConfiguration( _
         ConfigurationUserLevel.None)
      
      ' Create the section entry  
      ' in the <configSections> and the 
      ' related target section in <configuration>.
      If config.Sections("CustomSection") Is Nothing Then
         customSection = New CustomSection()
         config.Sections.Add("CustomSection", customSection)
         customSection.SectionInformation.ForceSave = True
         config.Save(ConfigurationSaveMode.Full)
      End If
   Catch err As ConfigurationErrorsException
      Console.WriteLine(err.ToString())
   End Try
End Sub

Hinweise

Um den ConfigurationSection Konstruktor zu verwenden, müssen Sie zuerst einen benutzerdefinierten Abschnittstyp definieren. Ein Beispiel finden Sie in der ConfigurationSection Klassenübersicht.

Gilt für: