ConfigurationManager.OpenMappedExeConfiguration ConfigurationManager.OpenMappedExeConfiguration ConfigurationManager.OpenMappedExeConfiguration ConfigurationManager.OpenMappedExeConfiguration Method

定义

将指定的客户端配置文件作为 Configuration 对象打开。Opens the specified client configuration file as a Configuration object.

重载

OpenMappedExeConfiguration(ExeConfigurationFileMap, ConfigurationUserLevel) OpenMappedExeConfiguration(ExeConfigurationFileMap, ConfigurationUserLevel) OpenMappedExeConfiguration(ExeConfigurationFileMap, ConfigurationUserLevel) OpenMappedExeConfiguration(ExeConfigurationFileMap, ConfigurationUserLevel)

可将指定的客户端配置文件作为使用指定文件映射和用户级别的 Configuration 对象打开。Opens the specified client configuration file as a Configuration object that uses the specified file mapping and user level.

OpenMappedExeConfiguration(ExeConfigurationFileMap, ConfigurationUserLevel, Boolean) OpenMappedExeConfiguration(ExeConfigurationFileMap, ConfigurationUserLevel, Boolean) OpenMappedExeConfiguration(ExeConfigurationFileMap, ConfigurationUserLevel, Boolean) OpenMappedExeConfiguration(ExeConfigurationFileMap, ConfigurationUserLevel, Boolean)

可将指定的客户端配置文件作为使用指定文件映射、用户级别和预加载选项的 Configuration 对象打开。Opens the specified client configuration file as a Configuration object that uses the specified file mapping, user level, and preload option.

OpenMappedExeConfiguration(ExeConfigurationFileMap, ConfigurationUserLevel) OpenMappedExeConfiguration(ExeConfigurationFileMap, ConfigurationUserLevel) OpenMappedExeConfiguration(ExeConfigurationFileMap, ConfigurationUserLevel) OpenMappedExeConfiguration(ExeConfigurationFileMap, ConfigurationUserLevel)

可将指定的客户端配置文件作为使用指定文件映射和用户级别的 Configuration 对象打开。Opens the specified client configuration file as a Configuration object that uses the specified file mapping and user level.

public:
 static System::Configuration::Configuration ^ OpenMappedExeConfiguration(System::Configuration::ExeConfigurationFileMap ^ fileMap, System::Configuration::ConfigurationUserLevel userLevel);
public static System.Configuration.Configuration OpenMappedExeConfiguration (System.Configuration.ExeConfigurationFileMap fileMap, System.Configuration.ConfigurationUserLevel userLevel);
static member OpenMappedExeConfiguration : System.Configuration.ExeConfigurationFileMap * System.Configuration.ConfigurationUserLevel -> System.Configuration.Configuration
Public Shared Function OpenMappedExeConfiguration (fileMap As ExeConfigurationFileMap, userLevel As ConfigurationUserLevel) As Configuration

参数

fileMap
ExeConfigurationFileMap ExeConfigurationFileMap ExeConfigurationFileMap ExeConfigurationFileMap

一个 ExeConfigurationFileMap 对象,该对象引用代替应用程序的默认配置文件使用的配置文件。An ExeConfigurationFileMap object that references configuration file to use instead of the application default configuration file.

userLevel
ConfigurationUserLevel ConfigurationUserLevel ConfigurationUserLevel ConfigurationUserLevel

要打开配置的 ConfigurationUserLevel 对象。The ConfigurationUserLevel object for which you are opening the configuration.

返回

配置对象。The configuration object.

异常

示例

下面的代码示例演示如何使用OpenMappedExeConfiguration方法获取配置文件中包含的所有节。The following code example shows how to use the OpenMappedExeConfiguration method to obtain all sections that are contained by the configuration file.


   // Access a configuration file using mapping.
   // This function uses the OpenMappedExeConfiguration 
   // method to access a new configuration file.   
   // It also gets the custom ConsoleSection and 
   // sets its ConsoleElement BackgroundColor and
   // ForegroundColor properties to green and red
   // respectively. Then it uses these properties to
   // set the console colors.  
   public static void MapExeConfiguration()
   {

     // Get the application configuration file.
     System.Configuration.Configuration config =
       ConfigurationManager.OpenExeConfiguration(
             ConfigurationUserLevel.None);
   
     Console.WriteLine(config.FilePath);

     if (config == null)
     {
       Console.WriteLine(
         "The configuration file does not exist.");
       Console.WriteLine(
        "Use OpenExeConfiguration to create the file.");
     }

     // Create a new configuration file by saving 
     // the application configuration to a new file.
     string appName = 
       Environment.GetCommandLineArgs()[0];

     string configFile =  string.Concat(appName, 
       ".2.config");
     config.SaveAs(configFile, ConfigurationSaveMode.Full);

     // Map the new configuration file.
     ExeConfigurationFileMap configFileMap = 
         new ExeConfigurationFileMap();
     configFileMap.ExeConfigFilename = configFile;

     // Get the mapped configuration file
    config = 
       ConfigurationManager.OpenMappedExeConfiguration(
         configFileMap, ConfigurationUserLevel.None);

     // Make changes to the new configuration file. 
     // This is to show that this file is the 
     // one that is used.
     string sectionName = "consoleSection";

     ConsoleSection customSection =
       (ConsoleSection)config.GetSection(sectionName);

     if (customSection == null)
     {
         customSection = new ConsoleSection();
         config.Sections.Add(sectionName, customSection);
     }
     else
         // Change the section configuration values.
         customSection =
             (ConsoleSection)config.GetSection(sectionName);

     customSection.ConsoleElement.BackgroundColor =
         ConsoleColor.Green;
     customSection.ConsoleElement.ForegroundColor =
         ConsoleColor.Red;

     // 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);

     // Set console properties using the 
     // configuration values contained in the 
     // new configuration file.
     Console.BackgroundColor =
       customSection.ConsoleElement.BackgroundColor;
     Console.ForegroundColor =
       customSection.ConsoleElement.ForegroundColor;
     Console.Clear();

     Console.WriteLine();
     Console.WriteLine("Using OpenMappedExeConfiguration.");
     Console.WriteLine("Configuration file is: {0}", 
       config.FilePath);
   }
 

' Access a configuration file using mapping.
' This function uses the OpenMappedExeConfiguration 
' method to access a new configuration file.   
' It also gets the custom ConsoleSection and 
' sets its ConsoleElement BackgroundColor and
' ForegroundColor properties to green and red
' respectively. Then it uses these properties to
' set the console colors.  
Public Shared Sub MapExeConfiguration()

    ' Get the application configuration file.
    Dim config As System.Configuration.Configuration = _
    ConfigurationManager.OpenExeConfiguration( _
        ConfigurationUserLevel.None)

    Console.WriteLine(config.FilePath)

    If config Is Nothing Then
        Console.WriteLine( _
        "The configuration file does not exist.")
        Console.WriteLine( _
        "Use OpenExeConfiguration to create file.")
    End If

    ' Create a new configuration file by saving 
    ' the application configuration to a new file.
    Dim appName As String = _
        Environment.GetCommandLineArgs()(0)

    Dim configFile As String = _
        String.Concat(appName, "2.config")
    config.SaveAs(configFile, _
                  ConfigurationSaveMode.Full)

    ' Map the new configuration file.
    Dim configFileMap As New ExeConfigurationFileMap()
    configFileMap.ExeConfigFilename = configFile

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

    ' Make changes to the new configuration file. 
    ' This is to show that this file is the 
    ' one that is used.
    Dim sectionName As String = "consoleSection"

    Dim customSection As ConsoleSection = _
        DirectCast(config.GetSection(sectionName),  _
            ConsoleSection)

    If customSection Is Nothing Then
        customSection = New ConsoleSection()
        config.Sections.Add(sectionName, customSection)
    End If

    ' Change the section configuration values.
    customSection = _
        DirectCast(config.GetSection(sectionName),  _
            ConsoleSection)
    customSection.ConsoleElement.BackgroundColor = _
        ConsoleColor.Green
    customSection.ConsoleElement.ForegroundColor = _
        ConsoleColor.Red
    ' 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)

    ' Set console properties using the 
    ' configuration values contained in the 
    ' new configuration file.
    Console.BackgroundColor = _
        customSection.ConsoleElement.BackgroundColor
    Console.ForegroundColor = _
        customSection.ConsoleElement.ForegroundColor
    Console.Clear()

    Console.WriteLine()
    Console.WriteLine( _
        "Using OpenMappedExeConfiguration.")
    Console.WriteLine( _
        "Configuration file is: {0}", config.FilePath)
End Sub

注解

ConfigurationUserLevel对象确定正在打开的配置文件的位置。The ConfigurationUserLevel object determines the location of the configuration file being opened. 它指示文件是否没有用户级别(配置文件与应用程序在同一目录中)或是否具有每用户级别(配置文件位于由确定的userLevel应用程序设置路径中)。It indicates whether the file has no user level (the configuration file is in the same directory as the application) or has a per-user level (the configuration file is in an application settings path determined by userLevel).

备注

若要获取Configuration资源的对象,你的代码必须对它继承其设置的所有配置文件具有读取权限。To obtain the Configuration object for a resource, your code must have read permissions on all the configuration files from which it inherits settings. 若要更新配置文件,你的代码还必须对配置文件及其所在的目录具有写入权限。To update a configuration file, your code must additionally have write permissions for both the configuration file and the directory in which it exists.

另请参阅

OpenMappedExeConfiguration(ExeConfigurationFileMap, ConfigurationUserLevel, Boolean) OpenMappedExeConfiguration(ExeConfigurationFileMap, ConfigurationUserLevel, Boolean) OpenMappedExeConfiguration(ExeConfigurationFileMap, ConfigurationUserLevel, Boolean) OpenMappedExeConfiguration(ExeConfigurationFileMap, ConfigurationUserLevel, Boolean)

可将指定的客户端配置文件作为使用指定文件映射、用户级别和预加载选项的 Configuration 对象打开。Opens the specified client configuration file as a Configuration object that uses the specified file mapping, user level, and preload option.

public:
 static System::Configuration::Configuration ^ OpenMappedExeConfiguration(System::Configuration::ExeConfigurationFileMap ^ fileMap, System::Configuration::ConfigurationUserLevel userLevel, bool preLoad);
public static System.Configuration.Configuration OpenMappedExeConfiguration (System.Configuration.ExeConfigurationFileMap fileMap, System.Configuration.ConfigurationUserLevel userLevel, bool preLoad);
static member OpenMappedExeConfiguration : System.Configuration.ExeConfigurationFileMap * System.Configuration.ConfigurationUserLevel * bool -> System.Configuration.Configuration
Public Shared Function OpenMappedExeConfiguration (fileMap As ExeConfigurationFileMap, userLevel As ConfigurationUserLevel, preLoad As Boolean) As Configuration

参数

fileMap
ExeConfigurationFileMap ExeConfigurationFileMap ExeConfigurationFileMap ExeConfigurationFileMap

一个 ExeConfigurationFileMap 对象,该对象引用代替应用程序的默认配置文件使用的配置文件。An ExeConfigurationFileMap object that references the configuration file to use instead of the default application configuration file.

userLevel
ConfigurationUserLevel ConfigurationUserLevel ConfigurationUserLevel ConfigurationUserLevel

要打开配置的 ConfigurationUserLevel 对象。The ConfigurationUserLevel object for which you are opening the configuration.

preLoad
Boolean Boolean Boolean Boolean

如果预加载所有部分团队和部分,则为 true;否则为 falsetrue to preload all section groups and sections; otherwise, false.

返回

配置对象。The configuration object.

异常

注解

ConfigurationUserLevel 对象确定将打开的配置文件的位置。The ConfigurationUserLevel object determines the location of the configuration file that is being opened. 它指示该文件是否具有用户级别(此配置文件位于应用程序所在的目录中)或每用户级别(此配置文件位于由 userLevel 决定的应用程序设置路径中)。It indicates whether the file has no user level (the configuration file is in the same directory as the application) or has a per-user level (the configuration file is in an application settings path that is determined by userLevel).

备注

若要获取Configuration资源的对象,你的代码必须对它继承其设置的所有配置文件具有读取权限。To obtain the Configuration object for a resource, your code must have read permissions on all the configuration files from which it inherits settings. 若要更新配置文件,你的代码还必须对配置文件及其所在的目录具有写入权限。To update a configuration file, your code must additionally have write permissions for both the configuration file and the directory in which it exists.

有关示例代码,请参见 OpenMappedExeConfiguration 重载。For a code example, see the OpenMappedExeConfiguration overload.

适用于