Export (0) Print
Expand All

ConfigurationManager.OpenMachineConfiguration Method

Opens the machine configuration file on the current computer as a Configuration object.

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

public static Configuration OpenMachineConfiguration()

ExceptionCondition
ConfigurationErrorsException

A configuration file could not be loaded.

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 the .NET Framework. The Machine.config file is located in the following subdirectory:

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

NoteNote

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.

The following code example shows how to use the OpenMachineConfiguration method to obtain all sections that are contained 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);
     }

   }

.NET Framework

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

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

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.

Show:
© 2014 Microsoft