Επεξεργασία

Κοινή χρήση μέσω


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.

public:
 static System::Configuration::Configuration ^ OpenMachineConfiguration();
public static System.Configuration.Configuration OpenMachineConfiguration ();
static member OpenMachineConfiguration : unit -> System.Configuration.Configuration
Public Shared Function OpenMachineConfiguration () As Configuration

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.


// 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);
}
' Show how to use OpenMachineConfiguration().
' It gets the machine.config file on the current 
' machine and displays section information. 
Shared Sub OpenMachineConfiguration1()
   ' Get the machine.config file on the current machine.
     Dim config As System.Configuration.Configuration = _
     WebConfigurationManager.OpenMachineConfiguration()
   
   ' 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

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

OpenMachineConfiguration(String)

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

public:
 static System::Configuration::Configuration ^ OpenMachineConfiguration(System::String ^ locationSubPath);
public static System.Configuration.Configuration OpenMachineConfiguration (string locationSubPath);
static member OpenMachineConfiguration : string -> System.Configuration.Configuration
Public Shared Function OpenMachineConfiguration (locationSubPath As String) As Configuration

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.


// 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);
}
' Show how to use OpenMachineConfiguration(string).
' It gets the machine.config file applicabe to the
' specified resource and displays section 
' basic information. 
Shared Sub OpenMachineConfiguration2()
    ' Get the machine.config file applicabe to the
    ' specified reosurce.
    Dim config As System.Configuration.Configuration = _
    WebConfigurationManager.OpenMachineConfiguration( _
    "configTest")

    ' 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

Remarks

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

See also

Applies to

OpenMachineConfiguration(String, String)

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

public:
 static System::Configuration::Configuration ^ OpenMachineConfiguration(System::String ^ locationSubPath, System::String ^ server);
public static System.Configuration.Configuration OpenMachineConfiguration (string locationSubPath, string server);
static member OpenMachineConfiguration : string * string -> System.Configuration.Configuration
Public Shared Function OpenMachineConfiguration (locationSubPath As String, server As String) As Configuration

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.


// 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);
}
' Show how to use OpenMachineConfiguration(string, string).
' It gets the machine.config file on the specified server and
' applicabe to the specified reosurce and displays section 
' basic information. 
Shared Sub OpenMachineConfiguration3()
    ' Get the machine.config file applicabe to the
    ' specified reosurce and on the specified server.
    Dim config As System.Configuration.Configuration = _
    WebConfigurationManager.OpenMachineConfiguration( _
    "configTest", "myServer")

    ' 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

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

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.

public:
 static System::Configuration::Configuration ^ OpenMachineConfiguration(System::String ^ locationSubPath, System::String ^ server, IntPtr userToken);
public static System.Configuration.Configuration OpenMachineConfiguration (string locationSubPath, string server, IntPtr userToken);
static member OpenMachineConfiguration : string * string * nativeint -> System.Configuration.Configuration
Public Shared Function OpenMachineConfiguration (locationSubPath As String, server As String, userToken As IntPtr) As Configuration

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

nativeint

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.


// 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);
}
' Show how to use OpenMachineConfiguration(string, string).
' It gets the machine.config file on the specified server,
' applicabe to the specified reosurce, for the specified user
' and displays section basic information. 
Shared Sub OpenMachineConfiguration4()
    ' Get the current user token.
    Dim userToken As IntPtr = _
    System.Security.Principal.WindowsIdentity.GetCurrent().Token

    ' Get the machine.config file applicabe to the
    ' specified reosurce, on the specified server for the
    ' specified user.
    Dim config As System.Configuration.Configuration = _
    WebConfigurationManager.OpenMachineConfiguration( _
    "configTest", "myServer", userToken)

    ' 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

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

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.

public:
 static System::Configuration::Configuration ^ OpenMachineConfiguration(System::String ^ locationSubPath, System::String ^ server, System::String ^ userName, System::String ^ password);
public static System.Configuration.Configuration OpenMachineConfiguration (string locationSubPath, string server, string userName, string password);
static member OpenMachineConfiguration : string * string * string * string -> System.Configuration.Configuration
Public Shared Function OpenMachineConfiguration (locationSubPath As String, server As String, userName As String, password As String) As Configuration

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.


// 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);
}
 ' Show how to use OpenMachineConfiguration(string, string).
 ' It gets the machine.config file on the specified server,
 ' applicabe to the specified reosurce, for the specified user
 ' and displays section basic information. 
 Shared Sub OpenMachineConfiguration5()
     ' Set the user id and password.
     Dim user As String = _
     System.Security.Principal.WindowsIdentity.GetCurrent().Name
     ' Substitute with actual password.
     Dim password As String = "userPassword"

     ' Get the machine.config file applicabe to the
     ' specified reosurce, on the specified server for the
     ' specified user.
     Dim config As System.Configuration.Configuration = _
     WebConfigurationManager.OpenMachineConfiguration( _
     "configTest", "myServer", user, password)

     ' 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

Remarks

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

See also

Applies to