System.Configuration.ConfigurationManager.OpenExeConfiguration 方法 (ConfigurationUserLevel)

方法描述

将当前应用程序的配置文件作为 Configuration 对象打开。

语法定义(C# System.Configuration.ConfigurationManager.OpenExeConfiguration 方法 (ConfigurationUserLevel) 的用法)

public static Configuration OpenExeConfiguration(
	ConfigurationUserLevel userLevel
)

参数/返回值

参数值/返回值 参数类型/返回类型 参数描述/返回描述
userLevel System-Configuration-ConfigurationUserLevel 要打开配置的 ConfigurationUserLevel。
返回值 System.Configuration.Configuration 一个 Configuration 对象。

提示和注释

客户端应用程序使用应用于所有用户的全局配置、应用于单个用户的单独配置以及应用于漫游用户的配置。 userLevel 参数通过指示该配置文件是不具有用户级别(配置文件与应用程序位于同一目录中),还是具有一个依每个用户而定的用户级别(配置文件位于用户级别所确定的应用程序设置路径中),从而确定所打开的配置文件的位置。

通过向 userLevel 传递下列值之一来指定要获取的配置:

若要获取应用于所有用户的 Configuration 对象,请将 userLevel 设置为 None。

若要获取应用于当前用户的本地 Configuration 对象,请将 userLevel 设置为 PerUserRoamingAndLocal。

若要获取应用于当前用户的漫游 Configuration 对象,请将 userLevel 设置为 PerUserRoaming。

注意

若要获取资源的 Configuration 对象,您的代码必须对它从中继承设置的所有配置文件具有读权限。 若要更新配置文件,您的代码还必须对该配置文件及其所在目录具有写权限。

System.Configuration.ConfigurationManager.OpenExeConfiguration 方法 (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 ConsoleEment 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);
}

异常

异常 异常描述
ConfigurationErrorsException 未能加载配置文件。

命名空间

namespace: System.Configuration

程序集: System.Configuration(在 System.Configuration.dll 中)

版本信息

.NET Framework 受以下版本支持:4、3.5、3.0、2.0 .NET Framework Client Profile 受以下版本支持:4、3.5 SP1

适用平台

Windows 7, Windows Vista SP1 或更高版本, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008(不支持服务器核心), Windows Server 2008 R2(支持 SP1 或更高版本的服务器核心), Windows Server 2003 SP2 .NET Framework 并不是对每个平台的所有版本都提供支持。有关支持的版本的列表,请参见.NET Framework 系统要求。