Click to Rate and Give Feedback
MSDN
MSDN Library
.NET Development
Previous Versions
.NET Framework 2.0
 OpenMappedExeConfiguration Method
Collapse All/Expand All Collapse All
This page is specific to
Microsoft Visual Studio 2005/.NET Framework 2.0

Other versions are also available for the following:
.NET Framework Class Library
ConfigurationManager.OpenMappedExeConfiguration Method

Note: This method is new in the .NET Framework version 2.0.

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

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

Visual Basic (Declaration)
Public Shared Function OpenMappedExeConfiguration ( _
    fileMap As ExeConfigurationFileMap, _
    userLevel As ConfigurationUserLevel _
) As Configuration
Visual Basic (Usage)
Dim fileMap As ExeConfigurationFileMap
Dim userLevel As ConfigurationUserLevel
Dim returnValue As Configuration

returnValue = ConfigurationManager.OpenMappedExeConfiguration(fileMap, userLevel)
C#
public static Configuration OpenMappedExeConfiguration (
    ExeConfigurationFileMap fileMap,
    ConfigurationUserLevel userLevel
)
C++
public:
static Configuration^ OpenMappedExeConfiguration (
    ExeConfigurationFileMap^ fileMap, 
    ConfigurationUserLevel userLevel
)
J#
public static Configuration OpenMappedExeConfiguration (
    ExeConfigurationFileMap fileMap, 
    ConfigurationUserLevel userLevel
)
JScript
public static function OpenMappedExeConfiguration (
    fileMap : ExeConfigurationFileMap, 
    userLevel : ConfigurationUserLevel
) : Configuration

Parameters

fileMap

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

userLevel

The ConfigurationUserLevel object for which you are opening the configuration.

Return Value

A Configuration object.
Exception typeCondition

ConfigurationErrorsException

A configuration file could not be loaded.

The ConfigurationUserLevel object 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 userLevel).

NoteNote

To obtain the Configuration object for a resource, your code must have read privileges on all of the configuration files from which it inherits settings. To update a configuration file, your code must additionally have write privileges 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 contained by the configuration file.

Visual Basic
' Get all the sections in the mapped exe.config.
' Show how to use the 
' GetMappedExeConfigurationSections(fileMap, userLevel) method.
Shared Sub GetMappedExeConfigurationSections()
   ' Get the machine.config file.
   Dim fileMap As New ExeConfigurationFileMap()
   ' You may want to map to your own exe.comfig file here.
     fileMap.ExeConfigFilename = _
     "C:\test\ConfigurationManager.exe.config"
     Dim config As System.Configuration.Configuration = _
     ConfigurationManager.OpenMappedExeConfiguration(fileMap, _
     ConfigurationUserLevel.None)
   
   ' Loop to get the sections. Display basic information.
   Console.WriteLine("Name, Allow Definition")
   Dim i As Integer = 0
   Dim section As ConfigurationSection
   For Each section In  config.Sections
         Console.WriteLine(( _
         section.SectionInformation.Name + _
         ControlChars.Tab + _
         section.SectionInformation.AllowExeDefinition.ToString()))
      i += 1
   Next section 
   Console.WriteLine("[Total number of sections: {0]", i)
   
   ' Display machine.config path.
   Console.WriteLine("[File path: {0]", config.FilePath)
End Sub 'GetMappedExeConfigurationSections
 
C#
// Get all the sections in the mapped exe.config.
// Show how to use the 
// GetMappedExeConfigurationSections(fileMap, userLevel) method.
static void GetMappedExeConfigurationSections()
{
    // Get the machine.config file.
    ExeConfigurationFileMap fileMap =
        new ExeConfigurationFileMap();
    // You may want to map to your own exe.comfig file here.
    fileMap.ExeConfigFilename = 
        @"C:\test\ConfigurationManager.exe.config";
    System.Configuration.Configuration config =
        ConfigurationManager.OpenMappedExeConfiguration(fileMap, 
        ConfigurationUserLevel.None);

    // Loop to get the sections. Display basic information.
    Console.WriteLine("Name, Allow Definition");
    int i = 0;
    foreach (ConfigurationSection section in config.Sections)
    {
        Console.WriteLine(
            section.SectionInformation.Name + "\t" +
        section.SectionInformation.AllowExeDefinition);
        i += 1;

    
    Console.WriteLine("[Total number of sections: {0]", i);

    // Display machine.config path.
    Console.WriteLine("[File path: {0]", config.FilePath);

Windows 98, Windows 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

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

.NET Framework

Supported in: 2.0
Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
one more question      shanli   |   Edit   |   Show History
This is very helpful when I have a dll which called by different exe apps, they can share a common config file.
My question is will it cause any file access conflict on this shared config file? (I only read from the config file, no write action)
Tags What's this?: Add a tag
Flag as ContentBug
Processing
© 2009 Microsoft Corporation. All rights reserved. Terms of Use | Trademarks | Privacy Statement
Page view tracker