다음을 통해 공유


ConfigurationManager.OpenExeConfiguration 메서드

정의

지정된 클라이언트 구성 파일을 Configuration 개체로 엽니다.

오버로드

OpenExeConfiguration(ConfigurationUserLevel)

현재 애플리케이션의 구성 파일을 Configuration 개체로 엽니다.

OpenExeConfiguration(String)

지정된 클라이언트 구성 파일을 Configuration 개체로 엽니다.

OpenExeConfiguration(ConfigurationUserLevel)

Source:
ConfigurationManager.cs
Source:
ConfigurationManager.cs
Source:
ConfigurationManager.cs

현재 애플리케이션의 구성 파일을 Configuration 개체로 엽니다.

public:
 static System::Configuration::Configuration ^ OpenExeConfiguration(System::Configuration::ConfigurationUserLevel userLevel);
public static System.Configuration.Configuration OpenExeConfiguration (System.Configuration.ConfigurationUserLevel userLevel);
static member OpenExeConfiguration : System.Configuration.ConfigurationUserLevel -> System.Configuration.Configuration
Public Shared Function OpenExeConfiguration (userLevel As ConfigurationUserLevel) As Configuration

매개 변수

userLevel
ConfigurationUserLevel

구성을 열 사용자 수준을 지정하는 열거형 값 중 하나입니다.

반환

현재 애플리케이션의 구성 파일입니다.

예외

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

예제

다음 코드 예제에서는 OpenExeConfiguration 메서드를 사용하는 방법을 보여 줍니다.

// Get the roaming configuration file associated 
// with the application.
// This function uses the OpenExeConfiguration(
// ConfigurationUserLevel userLevel) method to 
// get the configuration file.
// It also creates a custom ConsoleSection and 
// sets its ConsoleElement BackgroundColor and
// ForegroundColor properties to blue and yellow
// respectively. Then it uses these properties to
// set the console colors.  
public static void GetRoamingConfiguration()
{
  // Define the custom section to add to the
  // configuration file.
  string sectionName = "consoleSection";
  ConsoleSection currentSection = null;
  
  // Get the roaming configuration 
  // that applies to the current user.
  Configuration roamingConfig =
    ConfigurationManager.OpenExeConfiguration(
     ConfigurationUserLevel.PerUserRoaming);

  // Map the roaming configuration file. This
  // enables the application to access 
  // the configuration file using the
  // System.Configuration.Configuration class
  ExeConfigurationFileMap configFileMap =
    new ExeConfigurationFileMap();
  configFileMap.ExeConfigFilename = 
    roamingConfig.FilePath;

  // Get the mapped configuration file.
  Configuration config =
    ConfigurationManager.OpenMappedExeConfiguration(
      configFileMap, ConfigurationUserLevel.None);
  
  try
    {
      currentSection =
           (ConsoleSection)config.GetSection(
             sectionName);

      // Synchronize the application configuration
      // if needed. The following two steps seem
      // to solve some out of synch issues 
      // between roaming and default
      // configuration.
      config.Save(ConfigurationSaveMode.Modified);

      // Force a reload of the changed section, 
      // if needed. This makes the new values available 
      // for reading.
      ConfigurationManager.RefreshSection(sectionName);

      if (currentSection == null)
      {
        // Create a custom configuration section.
        currentSection = new ConsoleSection();

        // Define where in the configuration file 
        // hierarchy the associated 
        // configuration section can be declared.
        // The following assignment assures that 
        // the configuration information can be 
        // defined in the user.config file in the 
        // roaming user directory. 
        currentSection.SectionInformation.AllowExeDefinition =
          ConfigurationAllowExeDefinition.MachineToLocalUser;

        // Allow the configuration section to be 
        // overridden by lower-level configuration files.
        // This means that lower-level files can contain
        // the section (use the same name) and assign 
        // different values to it as done by the
        // function GetApplicationConfiguration() in this
        // example.
        currentSection.SectionInformation.AllowOverride =
          true;

        // Store console settings for roaming users.
        currentSection.ConsoleElement.BackgroundColor =
            ConsoleColor.Blue;
        currentSection.ConsoleElement.ForegroundColor =
            ConsoleColor.Yellow;

        // Add configuration information to 
        // the configuration file.
        config.Sections.Add(sectionName, currentSection);
        config.Save(ConfigurationSaveMode.Modified);
        // Force a reload of the changed section. This 
        // makes the new values available for reading.
        ConfigurationManager.RefreshSection(
          sectionName);
      }
  }
  catch (ConfigurationErrorsException e)
  {
      Console.WriteLine("[Exception error: {0}]",
          e.ToString());
  }

  // Set console properties using values
  // stored in the configuration file.
  Console.BackgroundColor =
    currentSection.ConsoleElement.BackgroundColor;
  Console.ForegroundColor =
    currentSection.ConsoleElement.ForegroundColor;
  // Apply the changes.
  Console.Clear();

  // Display feedback.
  Console.WriteLine();
  Console.WriteLine(
    "Using OpenExeConfiguration(ConfigurationUserLevel).");
  Console.WriteLine(
      "Configuration file is: {0}", config.FilePath);
}
    ' Get the roaming configuration file associated 
    ' with the application.
    ' This function uses the OpenExeConfiguration(
    ' ConfigurationUserLevel userLevel) method to 
    ' get the configuration file.
    ' It also creates a custom ConsoleSection and 
' sets its ConsoleElement BackgroundColor and
    ' ForegroundColor properties to blue and yellow
    ' respectively. Then it uses these properties to
    ' set the console colors.  
    Public Shared Sub GetRoamingConfiguration()
        ' Define the custom section to add to the
        ' configuration file.
        Dim sectionName As String = "consoleSection"
        Dim currentSection As ConsoleSection = Nothing

        ' Get the roaming configuration 
        ' that applies to the current user.
        Dim roamingConfig As Configuration = _
        ConfigurationManager.OpenExeConfiguration( _
            ConfigurationUserLevel.PerUserRoaming)

        ' Map the roaming configuration file. This
        ' enables the application to access 
        ' the configuration file using the
        ' System.Configuration.Configuration class
        Dim configFileMap As New ExeConfigurationFileMap()
        configFileMap.ExeConfigFilename = _
            roamingConfig.FilePath

        ' Get the mapped configuration file.
        Dim config As Configuration = _
            ConfigurationManager.OpenMappedExeConfiguration( _
                configFileMap, ConfigurationUserLevel.None)

        Try
            currentSection = DirectCast( _
                config.GetSection(sectionName),  _
                ConsoleSection)

            ' Synchronize the application configuration
            ' if needed. The following two steps seem
            ' to solve some out of synch issues 
            ' between roaming and default
            ' configuration.
            config.Save(ConfigurationSaveMode.Modified)

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

            If currentSection Is Nothing Then
                ' Create a custom configuration section.
                currentSection = New ConsoleSection()

                ' Define where in the configuration file 
                ' hierarchy the associated 
                ' configuration section can be declared.
                ' The following assignment assures that 
                ' the configuration information can be 
                ' defined in the user.config file in the 
                ' roaming user directory. 
                currentSection.SectionInformation. _
                AllowExeDefinition = _
                    ConfigurationAllowExeDefinition. _
                    MachineToLocalUser

                ' Allow the configuration section to be 
                ' overridden by lower-level configuration 
                ' files.
                ' This means that lower-level files can 
                ' contain()the section (use the same name) 
                ' and assign different values to it as 
                ' done by the function 
                ' GetApplicationConfiguration() in this
                ' example.
                currentSection.SectionInformation. _
                    AllowOverride = True

                ' Store console settings for roaming users.
                currentSection.ConsoleElement. _
                BackgroundColor = ConsoleColor.Blue
                currentSection.ConsoleElement. _
                ForegroundColor = ConsoleColor.Yellow

                ' Add configuration information to 
                ' the configuration file.
                config.Sections.Add(sectionName, _
                    currentSection)
                config.Save(ConfigurationSaveMode.Modified)
                ' Force a reload of the changed section. This 
                ' makes the new values available for reading.
                ConfigurationManager.RefreshSection( _
                    sectionName)
            End If
        Catch e As ConfigurationErrorsException
            Console.WriteLine("[Exception error: {0}]", _
                              e.ToString())
        End Try

        ' Set console properties using values
        ' stored in the configuration file.
        Console.BackgroundColor = _
            currentSection.ConsoleElement.BackgroundColor
        Console.ForegroundColor = _
            currentSection.ConsoleElement.ForegroundColor
        ' Apply the changes.
        Console.Clear()

        ' Display feedback.
        Console.WriteLine()
        Console.WriteLine( _
            "Using OpenExeConfiguration(userLevel).")
        Console.WriteLine( _
            "Configuration file is: {0}", config.FilePath)
    End Sub

설명

클라이언트 애플리케이션에는 모든 사용자, 개별 사용자에 게 적용 되는 별도 구성 및 로밍 사용자에 게 적용 되는 구성에 적용 되는 전역 구성을 사용 합니다. userLevel (구성 파일은 애플리케이션과 동일한 디렉터리에서) 사용자 수준이 없는지 여부를 지정 하 여 열려 있는 구성 파일의 위치를 결정 하는 매개 변수 사용자별 수준이 나 (구성 파일에는 애플리케이션 설정 경로 사용자 수준에 따라 결정).

에 대해 다음 값 중 하나를 전달하여 가져올 구성을 지정합니다 userLevel.

  • 모든 사용자에게 적용되는 개체를 얻으려면 Configuration 를 로 None설정합니다userLevel.

  • 현재 사용자에게 적용되는 로컬 Configuration 개체를 얻으려면 를 로 설정합니다 userLevelPerUserRoamingAndLocal.

  • 현재 사용자에게 적용되는 로밍 Configuration 개체를 얻으려면 를 로 PerUserRoaming설정합니다userLevel.

    참고

    리소스에 대한 개체를 Configuration 얻으려면 코드에 설정을 상속하는 모든 구성 파일에 대한 읽기 권한이 있어야 합니다. 구성 파일을 업데이트하려면 코드에 구성 파일과 구성 파일이 있는 디렉터리 모두에 대한 쓰기 권한이 추가로 있어야 합니다.

추가 정보

적용 대상

OpenExeConfiguration(String)

Source:
ConfigurationManager.cs
Source:
ConfigurationManager.cs
Source:
ConfigurationManager.cs

지정된 클라이언트 구성 파일을 Configuration 개체로 엽니다.

public:
 static System::Configuration::Configuration ^ OpenExeConfiguration(System::String ^ exePath);
public static System.Configuration.Configuration OpenExeConfiguration (string exePath);
static member OpenExeConfiguration : string -> System.Configuration.Configuration
Public Shared Function OpenExeConfiguration (exePath As String) As Configuration

매개 변수

exePath
String

실행(exe) 파일의 경로입니다.

반환

지정된 구성 파일입니다.

예외

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

예제

다음 코드 예제에서는 OpenExeConfiguration 메서드를 사용하는 방법을 보여 줍니다.

    // Get the application configuration file.
    // This function uses the 
    // OpenExeConfiguration(string)method 
    // to get the application configuration file. 
    // It also creates a custom ConsoleSection and 
    // sets its ConsoleElement BackgroundColor and
    // ForegroundColor properties to black and white
    // respectively. Then it uses these properties to
    // set the console colors.  
    public static void GetAppConfiguration()
    {

      // Get the application path needed to obtain
      // the application configuration file.
#if DEBUG
      string applicationName =
          Environment.GetCommandLineArgs()[0];
#else
           string applicationName =
          Environment.GetCommandLineArgs()[0]+ ".exe";
#endif

      string exePath = System.IO.Path.Combine(
          Environment.CurrentDirectory, applicationName);

      // Get the configuration file. The file name has
      // this format appname.exe.config.
      System.Configuration.Configuration config =
        ConfigurationManager.OpenExeConfiguration(exePath);

      try
      {
        
        // Create a custom configuration section
        // having the same name that is used in the 
        // roaming configuration file.
        // This is because the configuration section 
        // can be overridden by lower-level 
        // configuration files. 
        // See the GetRoamingConfiguration() function in 
        // this example.
        string sectionName = "consoleSection";
        ConsoleSection customSection = new ConsoleSection();

        if (config.Sections[sectionName] == null)
        {
          // Create a custom section if it does 
          // not exist yet.
          
          // Store console settings.
          customSection.ConsoleElement.BackgroundColor =
              ConsoleColor.Black;
          customSection.ConsoleElement.ForegroundColor =
              ConsoleColor.White;

          // Add configuration information to the
          // configuration file.
          config.Sections.Add(sectionName, customSection);
          config.Save(ConfigurationSaveMode.Modified);
          // Force a reload of the changed section.
          // This makes the new values available for reading.
          ConfigurationManager.RefreshSection(sectionName);
        }
        // Set console properties using values
        // stored in the configuration file.
        customSection =
            (ConsoleSection)config.GetSection(sectionName);
        Console.BackgroundColor =
            customSection.ConsoleElement.BackgroundColor;
        Console.ForegroundColor =
            customSection.ConsoleElement.ForegroundColor;
        // Apply the changes.
        Console.Clear();
      }
      catch (ConfigurationErrorsException e)
      {
        Console.WriteLine("[Error exception: {0}]",
            e.ToString());
      }
     
      // Display feedback.
      Console.WriteLine();
      Console.WriteLine("Using OpenExeConfiguration(string).");
      // Display the current configuration file path.
      Console.WriteLine("Configuration file is: {0}", 
        config.FilePath);
    }
    ' Get the application configuration file.
    ' This function uses the 
    ' OpenExeConfiguration(string)method 
    ' to get the application configuration file. 
    ' It also creates a custom ConsoleSection and 
    ' sets its ConsoleElement BackgroundColor and
    ' ForegroundColor properties to black and white
    ' respectively. Then it uses these properties to
    ' set the console colors.  
    Public Shared Sub GetAppConfiguration()
        ' Get the application path needed to obtain
        ' the application configuration file.
#If DEBUG Then
        Dim applicationName As String = _
            Environment.GetCommandLineArgs()(0)
#Else
            Dim applicationName As String = _
                Environment.GetCommandLineArgs()(0) + ".exe"
#End If

        Dim exePath As String = _
        System.IO.Path.Combine( _
            Environment.CurrentDirectory, applicationName)

        ' Get the configuration file. The file name has
        ' this format appname.exe.config.
        Dim config As System.Configuration.Configuration = _
            ConfigurationManager.OpenExeConfiguration(exePath)

        Try

            ' Create a custom configuration section
            ' having the same name that is used in the 
            ' roaming configuration file.
            ' This is because the configuration section 
            ' can be overridden by lower-level 
            ' configuration files. 
            ' See the GetRoamingConfiguration() function in 
            ' this example.
            Dim sectionName As String = "consoleSection"
            Dim customSection As New ConsoleSection()

            If config.Sections(sectionName) Is Nothing Then
                ' Create a custom section if it does 
                ' not exist yet.

                ' Store console settings.
                customSection.ConsoleElement. _
                    BackgroundColor = ConsoleColor.Black
                customSection.ConsoleElement. _
                    ForegroundColor = ConsoleColor.White

                ' Add configuration information to the
                ' configuration file.
                config.Sections.Add(sectionName, _
                                    customSection)
                config.Save(ConfigurationSaveMode.Modified)
                ' Force a reload of the changed section.
                ' This makes the new values available 
                ' for reading.
                ConfigurationManager.RefreshSection( _
                    sectionName)
            End If
            ' Set console properties using values
            ' stored in the configuration file.
            customSection = DirectCast( _
                config.GetSection(sectionName),  _
                    ConsoleSection)
            Console.BackgroundColor = _
                customSection.ConsoleElement.BackgroundColor
            Console.ForegroundColor = _
                customSection.ConsoleElement.ForegroundColor
            ' Apply the changes.
            Console.Clear()
        Catch e As ConfigurationErrorsException
            Console.WriteLine("[Error exception: {0}]", _
                              e.ToString())
        End Try

        ' Display feedback.
        Console.WriteLine()
        Console.WriteLine( _
            "Using OpenExeConfiguration(string).")
        ' Display the current configuration file path.
        Console.WriteLine( _
            "Configuration file is: {0}", config.FilePath)
    End Sub

설명

이 메서드 오버로드를 호출하는 것은 매개 변수가 OpenMappedExeConfiguration(ExeConfigurationFileMap, ConfigurationUserLevel, Boolean) 로 설정된 오버로드를 호출하는 preLoad 것과 false같습니다.

추가 정보

적용 대상