Export (0) Print
Expand All
1 out of 3 rated this helpful - Rate this topic

ConfigurationManager.OpenMappedExeConfiguration Method (ExeConfigurationFileMap, ConfigurationUserLevel)

Opens the specified client configuration file as a Configuration object that uses the specified file mapping and user level.

Namespace:  System.Configuration
Assembly:  System.Configuration (in System.Configuration.dll)
public static Configuration OpenMappedExeConfiguration(
	ExeConfigurationFileMap fileMap,
	ConfigurationUserLevel userLevel


Type: System.Configuration.ExeConfigurationFileMap

An ExeConfigurationFileMap object that references configuration file to use instead of the application default configuration file.

Type: System.Configuration.ConfigurationUserLevel

The ConfigurationUserLevel object for which you are opening the configuration.

Return Value

Type: System.Configuration.Configuration
The configuration object.

A configuration file could not be loaded.

The ConfigurationUserLevel object determines the location of the configuration file being opened. 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).


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.

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 ConsoleEment 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 =


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

    // Create a new configuration file by saving  
    // the application configuration to a new file. 
    string appName = 

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

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

    // Get the mapped configuration file
   config = 
        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 =

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

    customSection.ConsoleElement.BackgroundColor =
    customSection.ConsoleElement.ForegroundColor =

    // Save the configuration file.

    // Force a reload of the changed section. This  
    // makes the new values available for reading.

    // Set console properties using the  
    // configuration values contained in the  
    // new configuration file.
    Console.BackgroundColor =
    Console.ForegroundColor =

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

.NET Framework

Supported in: 4.5.1, 4.5, 4, 3.5, 3.0, 2.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows Phone 8.1, Windows Phone 8, Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback
© 2014 Microsoft. All rights reserved.