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 对象。

例外

无法加载有效的配置文件。

示例

以下示例演示如何使用 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 对象。

例外

无法加载有效的配置文件。

示例

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


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

注解

此方法打开适用于参数指定的 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 对象。

例外

无法加载有效的配置文件。

示例

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


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

注解

此方法打开位于参数指定的 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 对象。

例外

未为 serveruserToken 参数提供有效值。

无法加载有效的配置文件。

示例

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


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

注解

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

注释

帐户令牌通常是从 WindowsIdentity 类的实例或通过调用非托管代码(例如调用 Windows API LogonUser)检索的。 有关调用非托管代码的详细信息,请参阅 使用非托管 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

打开文件时要使用的完整用户名(域\用户)。

password
String

对应于用户名的密码。

返回

Configuration 对象。

例外

serveruserName参数password无效。

无法加载有效的配置文件。

示例

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


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

注解

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

另请参阅

适用于