WebConfigurationManager.OpenMachineConfiguration Method

Definition

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

Overloads

OpenMachineConfiguration()

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

OpenMachineConfiguration(String)

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

OpenMachineConfiguration(String, String)

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

OpenMachineConfiguration(String, String, IntPtr)

Opens the specified machine-configuration file on the specified server as a Configuration object, using the specified security context to allow read or write operations.

OpenMachineConfiguration(String, String, String, String)

Opens the specified machine-configuration file on the specified server as a Configuration object, using the specified security context to allow read or write operations.

OpenMachineConfiguration()

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

C#
public static System.Configuration.Configuration OpenMachineConfiguration();

Returns

A Configuration object.

Exceptions

A valid configuration file could not be loaded.

Examples

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

C#

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

Remarks

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.

See also

Applies to

.NET Framework 4.8.1 and other versions
Product Versions
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1

OpenMachineConfiguration(String)

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

C#
public static System.Configuration.Configuration OpenMachineConfiguration(string locationSubPath);

Parameters

locationSubPath
String

The application path to which the machine configuration applies.

Returns

A Configuration object.

Exceptions

A valid configuration file could not be loaded.

Examples

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

C#

// Show how to use OpenMachineConfiguration(string).
// It gets the machine.config file applicable to the
// specified resource and displays section
// basic information.
static void OpenMachineConfiguration2()
{
    // Get the machine.config file applicable to the
    // specified resource.
    System.Configuration.Configuration config =
        WebConfigurationManager.OpenMachineConfiguration("configTest");

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

Remarks

This method opens the machine-configuration file that is applicable to the directory specified by the locationSubPath parameter.

See also

Applies to

.NET Framework 4.8.1 and other versions
Product Versions
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1

OpenMachineConfiguration(String, String)

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

C#
public static System.Configuration.Configuration OpenMachineConfiguration(string locationSubPath, string server);

Parameters

locationSubPath
String

The application path to which the configuration applies.

server
String

The fully qualified name of the server to return the configuration for.

Returns

A Configuration object.

Exceptions

A valid configuration file could not be loaded.

Examples

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

C#

// Show how to use OpenMachineConfiguration(string, string).
// It gets the machine.config file on the specified server and
// applicable to the specified resource and displays section
// basic information.
static void OpenMachineConfiguration3()
{
    // Get the machine.config file applicable to the
    // specified resource and on the specified server.
    System.Configuration.Configuration config =
        WebConfigurationManager.OpenMachineConfiguration("configTest",
        "myServer");

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

Remarks

This method opens the machine-configuration file that is located in the directory specified by the locationSubPath parameter and on the computer specified by the server parameter.

See also

Applies to

.NET Framework 4.8.1 and other versions
Product Versions
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1

OpenMachineConfiguration(String, String, IntPtr)

Opens the specified machine-configuration file on the specified server as a Configuration object, using the specified security context to allow read or write operations.

C#
public static System.Configuration.Configuration OpenMachineConfiguration(string locationSubPath, string server, IntPtr userToken);

Parameters

locationSubPath
String

The application path to which the configuration applies.

server
String

The fully qualified name of the server to return the configuration for.

userToken
IntPtr

An account token to use.

Returns

A Configuration object.

Exceptions

Valid values were not supplied for the server or userToken parameters.

A valid configuration file could not be loaded.

Examples

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

C#

// Show how to use OpenMachineConfiguration(string, string).
// It gets the machine.config file on the specified server,
// applicable to the specified resource, for the specified user
// and displays section basic information.
static void OpenMachineConfiguration4()
{
    // Get the current user token.
    IntPtr userToken =
          System.Security.Principal.WindowsIdentity.GetCurrent().Token;

    // Get the machine.config file applicable to the
    // specified resource, on the specified server for the
    // specified user.
    System.Configuration.Configuration config =
        WebConfigurationManager.OpenMachineConfiguration("configTest",
        "myServer", userToken);

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

Remarks

This method is used to access a configuration file using impersonation.

Note

The account token is usually retrieved from an instance of the WindowsIdentity class or through a call to unmanaged code, such as a call to the Windows API LogonUser. For more information about calls to unmanaged code, see Consuming Unmanaged DLL Functions.

See also

Applies to

.NET Framework 4.8.1 and other versions
Product Versions
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1

OpenMachineConfiguration(String, String, String, String)

Opens the specified machine-configuration file on the specified server as a Configuration object, using the specified security context to allow read or write operations.

C#
public static System.Configuration.Configuration OpenMachineConfiguration(string locationSubPath, string server, string userName, string password);

Parameters

locationSubPath
String

The application path to which the configuration applies.

server
String

The fully qualified name of the server to return the configuration for.

userName
String

The full user name (Domain\User) to use when opening the file.

password
String

The password for the user name.

Returns

A Configuration object.

Exceptions

The server or userName and password parameters were invalid.

A valid configuration file could not be loaded.

Examples

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

C#

// Show how to use OpenMachineConfiguration(string, string).
// It gets the machine.config file on the specified server,
// applicable to the specified resource, for the specified user
// and displays section basic information.
static void OpenMachineConfiguration5()
{
    // Set the user id and password.
    string user =
          System.Security.Principal.WindowsIdentity.GetCurrent().Name;
    // Substitute with actual password.
    string password = "userPassword";

    // Get the machine.config file applicable to the
    // specified resource, on the specified server for the
    // specified user.
    System.Configuration.Configuration config =
        WebConfigurationManager.OpenMachineConfiguration("configTest",
        "myServer", user, password);

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

Remarks

This method is used to access a configuration file using impersonation.

See also

Applies to

.NET Framework 4.8.1 and other versions
Product Versions
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1