ConfigurationManager.GetSection(String) 메서드

정의

현재 애플리케이션의 기본 구성에 대한 지정된 구성 섹션을 검색합니다.

public:
 static System::Object ^ GetSection(System::String ^ sectionName);
public static object GetSection (string sectionName);
static member GetSection : string -> obj
Public Shared Function GetSection (sectionName As String) As Object

매개 변수

sectionName
String

구성 섹션 경로와 이름입니다. 노드 이름은 슬래시(예: "system.net/mailSettings/smtp")로 구분됩니다.

반환

지정된 ConfigurationSection 개체이거나, 섹션이 없으면 null입니다.

예외

구성 파일을 로드할 수 없는 경우

예제

다음 예제에서는 GetSection 메서드를 사용하는 방법을 보여 줍니다. 예제는 클래스에 대해 제공되는 더 큰 예제의 ConfigurationManager 일부입니다.

// Create the AppSettings section.
// The function uses the GetSection(string)method 
// to access the configuration section. 
// It also adds a new element to the section collection.
public static void CreateAppSettings()
{
  // Get the application configuration file.
  System.Configuration.Configuration config =
    ConfigurationManager.OpenExeConfiguration(
          ConfigurationUserLevel.None);

  string sectionName = "appSettings";

  // Add an entry to appSettings.
  int appStgCnt =
      ConfigurationManager.AppSettings.Count;
  string newKey = "NewKey" + appStgCnt.ToString();

  string newValue = DateTime.Now.ToLongDateString() + 
    " " + DateTime.Now.ToLongTimeString();

  config.AppSettings.Settings.Add(newKey, newValue);

  // Save the configuration file.
  config.Save(ConfigurationSaveMode.Modified);
  
  // Force a reload of the changed section. This 
  // makes the new values available for reading.
  ConfigurationManager.RefreshSection(sectionName);

  // Get the AppSettings section.
  AppSettingsSection appSettingSection =
    (AppSettingsSection)config.GetSection(sectionName);

  Console.WriteLine();
  Console.WriteLine("Using GetSection(string).");
  Console.WriteLine("AppSettings section:");
  Console.WriteLine(
    appSettingSection.SectionInformation.GetRawXml());
}
' Create the AppSettings section.
' The function uses the GetSection(string)method 
' to access the configuration section. 
' It also adds a new element to the section collection.
Public Shared Sub CreateAppSettings()
    ' Get the application configuration file.
    Dim config As System.Configuration.Configuration = _
    ConfigurationManager.OpenExeConfiguration( _
        ConfigurationUserLevel.None)

    Dim sectionName As String = "appSettings"

    ' Add an entry to appSettings.
    Dim appStgCnt As Integer = _
        ConfigurationManager.AppSettings.Count
    Dim newKey As String = _
        "NewKey" + appStgCnt.ToString()

    Dim newValue As String = _
        DateTime.Now.ToLongDateString() + " " + _
        DateTime.Now.ToLongTimeString()

    config.AppSettings.Settings.Add(newKey, _
                                    newValue)

    ' Save the configuration file.
    config.Save(ConfigurationSaveMode.Modified)

    ' Force a reload of the changed section. This 
    ' makes the new values available for reading.
    ConfigurationManager.RefreshSection(sectionName)

    ' Get the AppSettings section.
    Dim appSettingSection As AppSettingsSection = _
        DirectCast(config.GetSection(sectionName),  _
        AppSettingsSection)

    Console.WriteLine()
    Console.WriteLine( _
        "Using GetSection(string).")
    Console.WriteLine( _
        "AppSettings section:")
    Console.WriteLine( _
        appSettingSection.SectionInformation.GetRawXml())
End Sub

설명

클라이언트 애플리케이션의 경우이 메서드는 애플리케이션 구성 파일, 로컬 사용자 구성 파일 및 로밍 구성 파일을 병합 하 여 가져온 구성 파일을 검색 합니다.

메서드는 GetSection 변경할 수 없는 런타임 구성 정보에 액세스합니다. 구성을 변경하려면 다음 방법 중 하나를 사용하여 가져오는 구성 파일에서 메서드를 사용합니다 GetSection .

상속자 참고

반환 값을 예상 구성 형식으로 캐스팅해야 합니다. 가능한 캐스팅 예외를 방지하려면 C#의 연산자 또는 Visual Basic의 asTryCast 함수와 같은 조건부 캐스팅 작업을 사용해야 합니다.

적용 대상

추가 정보