ConfigurationManager.OpenExeConfiguration Method (ConfigurationUserLevel)


Opens the configuration file for the current application as a Configuration object.

Namespace:   System.Configuration
Assembly:  System.Configuration (in System.Configuration.dll)

public static Configuration OpenExeConfiguration(
	ConfigurationUserLevel userLevel


Type: System.Configuration.ConfigurationUserLevel

The ConfigurationUserLevel for which you are opening the configuration.

Exception Condition

A configuration file could not be loaded.

Client applications use a global configuration that applies to all users, separate configurations that apply to individual users, and configurations that apply to roaming users. The userLevel parameter determines the location of the configuration file being opened by indicating whether it 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 the user level).

Specify which configuration to get by passing one of the following values for userLevel:

  • To get the Configuration object that applies to all users, set userLevel to None.

  • To get the local Configuration object that applies to the current user, set userLevel to PerUserRoamingAndLocal.

  • To get the roaming Configuration object that applies to the current user, set userLevel to PerUserRoaming.


    To get 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 OpenExeConfiguration method.

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

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

  // Get the mapped configuration file.
  Configuration config =
      configFileMap, ConfigurationUserLevel.None);

      currentSection =

      // Synchronize the application configuration
      // if needed. The following two steps seem
      // to solve some out of synch issues 
      // between roaming and default
      // configuration.

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

      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 =

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

        // Store console settings for roaming users.
        currentSection.ConsoleElement.BackgroundColor =
        currentSection.ConsoleElement.ForegroundColor =

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

  // Set console properties using values
  // stored in the configuration file.
  Console.BackgroundColor =
  Console.ForegroundColor =
  // Apply the changes.

  // Display feedback.
    "Using OpenExeConfiguration(ConfigurationUserLevel).");
      "Configuration file is: {0}", config.FilePath);

.NET Framework
Available since 2.0
Return to top