Afficher en anglais

Partage via


Configuration.GetSection(String) Méthode

Définition

Retourne l'objet ConfigurationSection spécifié.

C#
public System.Configuration.ConfigurationSection GetSection (string sectionName);

Paramètres

sectionName
String

Chemin d'accès de la section à retourner.

Retours

Objet ConfigurationSection spécifié ou null si la section demandée n’existe pas.

Exemples

L’exemple suivant montre comment utiliser la GetSection méthode pour accéder à une section personnalisée. Pour obtenir l’exemple de code complet qui définit une classe qui stocke des informations pour la CustomSection section , consultez la vue d’ensemble de la Configuration classe.

C#
// Show how to use the GetSection(string) method.
static void GetCustomSection()
{
    try
    {

        CustomSection customSection;

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

        customSection =
            config.GetSection("CustomSection") as CustomSection;

        Console.WriteLine("Section name: {0}", customSection.Name);
        Console.WriteLine("Url: {0}", customSection.Url);
        Console.WriteLine("Port: {0}", customSection.Port);
    }
    catch (ConfigurationErrorsException err)
    {
        Console.WriteLine("Using GetSection(string): {0}", err.ToString());
    }
}

Remarques

Les paramètres de configuration sont contenus dans des sections qui regroupent des paramètres similaires pour des raisons pratiques. La GetSection méthode récupère une section de configuration par son nom.

S’applique à

Produit Versions
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9