Edita

Comparteix a través de


ConfigurationManager.OpenMappedMachineConfiguration Method

Definition

Opens the machine configuration file as a Configuration object that uses the specified file mapping.

public:
 static System::Configuration::Configuration ^ OpenMappedMachineConfiguration(System::Configuration::ConfigurationFileMap ^ fileMap);
public static System.Configuration.Configuration OpenMappedMachineConfiguration (System.Configuration.ConfigurationFileMap fileMap);
static member OpenMappedMachineConfiguration : System.Configuration.ConfigurationFileMap -> System.Configuration.Configuration
Public Shared Function OpenMappedMachineConfiguration (fileMap As ConfigurationFileMap) As Configuration

Parameters

fileMap
ConfigurationFileMap

The configuration file to use instead of the application default configuration file.

Returns

The machine configuration file.

Exceptions

A configuration file could not be loaded.

Examples

The following code example shows how to use the OpenMappedMachineConfiguration method to obtain all sections in the configuration file.

   // Access the machine configuration file using mapping.
   // The function uses the OpenMappedMachineConfiguration 
   // method to access the machine configuration. 
   public static void MapMachineConfiguration()
   {
     // Get the machine.config file.
     Configuration machineConfig =
       ConfigurationManager.OpenMachineConfiguration();
     // Get the machine.config file path.
     ConfigurationFileMap configFile =
       new ConfigurationFileMap(machineConfig.FilePath);

     // Map the application configuration file to the machine 
     // configuration file.
     Configuration config =
       ConfigurationManager.OpenMappedMachineConfiguration(
         configFile);

     // Get the AppSettings section.
     AppSettingsSection appSettingSection =
       (AppSettingsSection)config.GetSection("appSettings");
     appSettingSection.SectionInformation.AllowExeDefinition =
         ConfigurationAllowExeDefinition.MachineToRoamingUser;

     // Display the configuration file sections.
     ConfigurationSectionCollection sections = 
       config.Sections;

     Console.WriteLine();
     Console.WriteLine("Using OpenMappedMachineConfiguration.");
     Console.WriteLine("Sections in machine.config:");

     // Get the sections in the machine.config.
     foreach (ConfigurationSection section in sections)
     {
         string name = section.SectionInformation.Name;
         Console.WriteLine("Name: {0}", name);
     }
   }
' Access the machine configuration file using mapping.
' The function uses the OpenMappedMachineConfiguration 
' method to access the machine configuration. 
Public Shared Sub MapMachineConfiguration()
    ' Get the machine.config file.
    Dim machineConfig As Configuration = _
        ConfigurationManager.OpenMachineConfiguration()
    ' Get the machine.config file path.
    Dim configFile _
        As New ConfigurationFileMap( _
            machineConfig.FilePath)

    ' Map the application configuration file 
    ' to the machine configuration file.
    Dim config As Configuration = _
        ConfigurationManager. _
        OpenMappedMachineConfiguration( _
            configFile)

    ' Get the AppSettings section.
    Dim appSettingSection As AppSettingsSection = _
        DirectCast(config.GetSection("appSettings"),  _
            AppSettingsSection)
    appSettingSection.SectionInformation. _
    AllowExeDefinition = _
        ConfigurationAllowExeDefinition. _
        MachineToRoamingUser

    ' Display the configuration file sections.
    Dim sections As  _
        ConfigurationSectionCollection = _
        config.Sections

    Console.WriteLine()
    Console.WriteLine( _
        "Using OpenMappedMachineConfiguration.")
    Console.WriteLine( _
        "Sections in machine.config:")

    ' Get the sections in the machine.config.
    For Each section _
        As ConfigurationSection In sections
        Dim name As String = _
            section.SectionInformation.Name
        Console.WriteLine("Name: {0}", name)
    Next

End Sub

Remarks

Machine configuration settings apply to the whole computer and all applications that reside on it, unless overridden for the local application. Machine configuration settings are read from the Machine.config file of the currently running version of .NET Framework. The Machine.config file is located in the following subdirectory:

%windir%\Microsoft.NET\Framework\version\config

Note

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. It is not possible to access the Machine.config file for other versions of the .NET Framework that might be installed on the computer.

Applies to

See also