WebConfigurationManager.OpenMachineConfiguration 方法

定义

将计算机配置文件作为 Configuration 对象打开以允许读或写操作。

重载

OpenMachineConfiguration()

将当前计算机上的计算机配置文件作为 Configuration 对象打开以允许读或写操作。

OpenMachineConfiguration(String)

将当前计算机上的计算机配置文件作为 Configuration 对象打开以允许读或写操作。

OpenMachineConfiguration(String, String)

将指定服务器上的指定计算机配置文件作为 Configuration 对象打开以允许读或写操作。

OpenMachineConfiguration(String, String, IntPtr)

使用指定的安全上下文将指定服务器上的指定计算机配置文件作为 Configuration 对象打开以允许读或写操作。

OpenMachineConfiguration(String, String, String, String)

使用指定的安全上下文将指定服务器上的指定计算机配置文件作为 Configuration 对象打开以允许读或写操作。

OpenMachineConfiguration()

将当前计算机上的计算机配置文件作为 Configuration 对象打开以允许读或写操作。

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

返回

Configuration

Configuration 对象。

例外

未能加载有效的配置文件。

示例

以下示例演示如何使用 OpenMachineConfiguration 该方法访问配置信息。


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

注解

该方法 OpenMachineConfiguration 在运行应用程序的计算机上打开计算机配置文件。 此文件位于标准生成目录 %windir%\Microsoft.NET\Framework\version\config 中。

另请参阅

适用于

OpenMachineConfiguration(String)

将当前计算机上的计算机配置文件作为 Configuration 对象打开以允许读或写操作。

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

参数

locationSubPath
String

应用计算机配置的应用程序路径。

返回

Configuration

Configuration 对象。

例外

未能加载有效的配置文件。

示例

以下示例演示如何使用 OpenMachineConfiguration 该方法访问配置信息。


// Show how to use OpenMachineConfiguration(string).
// It gets the machine.config file applicabe to the
// specified resource and displays section 
// basic information. 
static void OpenMachineConfiguration2()
{
    // Get the machine.config file applicabe to the
    // specified reosurce.
    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

注解

此方法打开适用于参数指定的 locationSubPath 目录的计算机配置文件。

另请参阅

适用于

OpenMachineConfiguration(String, String)

将指定服务器上的指定计算机配置文件作为 Configuration 对象打开以允许读或写操作。

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

参数

locationSubPath
String

配置应用的应用程序路径。

server
String

完全限定服务器名,将返回该服务器的配置。

返回

Configuration

Configuration 对象。

例外

未能加载有效的配置文件。

示例

以下示例演示如何使用 OpenMachineConfiguration 该方法访问配置信息。


// 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. 
static void OpenMachineConfiguration3()
{
    // Get the machine.config file applicabe to the
    // specified reosurce 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

注解

此方法打开计算机配置文件,该文件位于参数指定的 locationSubPath 目录中,以及参数指定的 server 计算机上。

另请参阅

适用于

OpenMachineConfiguration(String, String, IntPtr)

使用指定的安全上下文将指定服务器上的指定计算机配置文件作为 Configuration 对象打开以允许读或写操作。

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

参数

locationSubPath
String

配置应用的应用程序路径。

server
String

完全限定服务器名,将返回该服务器的配置。

userToken
IntPtr

nativeint

要使用的帐户标记。

返回

Configuration

Configuration 对象。

例外

没有为 serveruserToken 参数提供有效值。

未能加载有效的配置文件。

示例

以下示例演示如何使用 OpenMachineConfiguration 该方法访问配置信息。


// 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. 
static void OpenMachineConfiguration4()
{
    // Get the current user token.
    IntPtr userToken =
          System.Security.Principal.WindowsIdentity.GetCurrent().Token;

    // Get the machine.config file applicabe to the
    // specified reosurce, 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

注解

此方法用于使用模拟访问配置文件。

备注

帐户令牌通常是从类的WindowsIdentity实例或通过调用非托管代码(例如调用 Windows APILogonUser)检索的。 有关调用非托管代码的详细信息,请参阅 使用非托管 DLL 函数

另请参阅

适用于

OpenMachineConfiguration(String, String, String, String)

使用指定的安全上下文将指定服务器上的指定计算机配置文件作为 Configuration 对象打开以允许读或写操作。

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

参数

locationSubPath
String

配置应用的应用程序路径。

server
String

完全限定服务器名,将返回该服务器的配置。

userName
String

打开文件时要使用的用户全名 (Domain\User)。

password
String

用户名的密码。

返回

Configuration

Configuration 对象。

例外

serveruserNamepassword 参数无效。

未能加载有效的配置文件。

示例

以下示例演示如何使用 OpenMachineConfiguration 该方法访问配置信息。


// 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. 
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 applicabe to the
    // specified reosurce, 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

注解

此方法用于使用模拟访问配置文件。

另请参阅

适用于