ConfigurationManager.OpenExeConfiguration 方法

定義

將指定的用戶端組態檔做為 Configuration 物件開啟。

多載

OpenExeConfiguration(ConfigurationUserLevel)

將目前應用程式的組態檔開啟為 Configuration 物件。

OpenExeConfiguration(String)

將指定的用戶端組態檔做為 Configuration 物件開啟。

OpenExeConfiguration(ConfigurationUserLevel)

來源:
ConfigurationManager.cs
來源:
ConfigurationManager.cs
來源:
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 適用于所有使用者的 物件,請將 設定 userLevelNone

  • 若要取得套用至目前使用者的本機 Configuration 物件,請將 設定 userLevelPerUserRoamingAndLocal

  • 若要取得套用至目前使用者的漫遊 Configuration 物件,請將 設定 userLevelPerUserRoaming

    注意

    若要取得 Configuration 資源的 物件,您的程式碼必須具有繼承設定之所有組態檔的讀取權限。 若要更新組態檔,您的程式碼必須另外具有組態檔及其所在目錄的寫入權限。

另請參閱

適用於

OpenExeConfiguration(String)

來源:
ConfigurationManager.cs
來源:
ConfigurationManager.cs
來源:
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

另請參閱

適用於