WebConfigurationManager.OpenMachineConfiguration Method ()

 

Opens the machine-configuration file on the current computer as a Configuration object to allow read or write operations.

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

public static Configuration OpenMachineConfiguration()

Exception Condition
ConfigurationErrorsException

A valid configuration file could not be loaded.

The OpenMachineConfiguration method opens the machine-configuration file on the computer where the application runs. This file is located in the standard build directory %windir%\Microsoft.NET\Framework\version\config.

The following example shows how to access configuration information with the OpenMachineConfiguration method.


// Show how to use OpenMachineConfiguration().
// It gets the machine.config file on the current 
// machine and displays section information. 
static void OpenMachineConfiguration1()
{
    // Get the machine.config file on the current machine.
    System.Configuration.Configuration config =
            WebConfigurationManager.OpenMachineConfiguration();

    // 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); 

}

.NET Framework
Available since 2.0
Return to top
Show: