WebConfigurationManager.OpenWebConfiguration 方法

定义

将 Web 应用程序配置文件作为 Configuration 对象打开。

重载

OpenWebConfiguration(String)

使用指定的虚拟路径将 Web 应用程序配置文件作为 Configuration 对象打开以允许读或写操作。

OpenWebConfiguration(String, String)

使用指定的虚拟路径和站点名称将 Web 应用程序配置文件作为 Configuration 对象打开以允许读或写操作。

OpenWebConfiguration(String, String, String)

使用指定的虚拟路径、网站名称和位置将 Web 应用程序配置文件作为 Configuration 对象打开以允许读或写操作。

OpenWebConfiguration(String, String, String, String)

使用指定的虚拟路径、站点名称、位置和服务器将 Web 应用程序配置文件作为 Configuration 对象打开以允许读或写操作。

OpenWebConfiguration(String, String, String, String, IntPtr)

使用指定的虚拟路径、站点名称、位置、服务器和安全上下文将 Web 应用程序配置文件作为 Configuration 对象打开以允许读或写操作。

OpenWebConfiguration(String, String, String, String, String, String)

使用指定的虚拟路径、站点名称、位置、服务器和安全上下文将 Web 应用程序配置文件作为 Configuration 对象打开以允许读或写操作。

OpenWebConfiguration(String)

使用指定的虚拟路径将 Web 应用程序配置文件作为 Configuration 对象打开以允许读或写操作。

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

参数

path
String

配置文件的虚拟路径。 如果为 null,则打开根 Web.config 文件。

返回

Configuration 对象。

例外

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

示例

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


// Show how to use OpenWebConfiguration(string).
// It gets he appSettings section of a Web application 
// runnig on the local server. 
static void OpenWebConfiguration1()
{
    // Get the configuration object for a Web application
    // running on the local server. 
    System.Configuration.Configuration config =
        WebConfigurationManager.OpenWebConfiguration("/configTest") 
        as System.Configuration.Configuration; 

    // Get the appSettings.
    KeyValueConfigurationCollection appSettings =
         config.AppSettings.Settings;

    // Loop through the collection and
    // display the appSettings key, value pairs.
    Console.WriteLine("[appSettings for app at: {0}]", "/configTest");
    foreach (string key in appSettings.AllKeys)
    {
        Console.WriteLine("Name: {0} Value: {1}",
        key, appSettings[key].Value);
    }

    Console.WriteLine();
}
' Show how to use OpenWebConfiguration(string).
' It gets he appSettings section of a Web application 
' runnig on the local server. 
Shared Sub OpenWebConfiguration1()
   ' Get the configuration object for a Web application
   ' running on the local server. 
     Dim config As System.Configuration.Configuration = _
     WebConfigurationManager.OpenWebConfiguration("/configTest")
   
   ' Get the appSettings.
     Dim appSettings As KeyValueConfigurationCollection = _
     config.AppSettings.Settings
   
   
   ' Loop through the collection and
   ' display the appSettings key, value pairs.
   Console.WriteLine("[appSettings for app at: {0}]", "/configTest")
   Dim key As String
   For Each key In  appSettings.AllKeys
         Console.WriteLine("Name: {0} Value: {1}", _
         key, appSettings(key).Value)
   Next key
   
   Console.WriteLine()
End Sub

注解

若要获取 Configuration 资源的 对象,代码必须对其从中继承设置的所有配置文件具有读取权限。 若要更新配置文件,代码还必须对配置文件及其所在的目录具有写入权限。

另请参阅

适用于

OpenWebConfiguration(String, String)

使用指定的虚拟路径和站点名称将 Web 应用程序配置文件作为 Configuration 对象打开以允许读或写操作。

public:
 static System::Configuration::Configuration ^ OpenWebConfiguration(System::String ^ path, System::String ^ site);
public static System.Configuration.Configuration OpenWebConfiguration (string path, string site);
static member OpenWebConfiguration : string * string -> System.Configuration.Configuration
Public Shared Function OpenWebConfiguration (path As String, site As String) As Configuration

参数

path
String

配置文件的虚拟路径。

site
String

显示在 Internet 信息服务 (IIS) 配置中的应用程序网站的名称。

返回

Configuration 对象。

例外

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

示例

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


// Show how to use OpenWebConfiguration(string, string).
// It gets he appSettings section of a Web application 
// runnig on the local server. 
static void OpenWebConfiguration2()
{
    // Get the configuration object for a Web application
    // running on the local server. 
    System.Configuration.Configuration config =
        WebConfigurationManager.OpenWebConfiguration("/configTest", 
        "Default Web Site")
        as System.Configuration.Configuration;

    // Get the appSettings.
    KeyValueConfigurationCollection appSettings =
         config.AppSettings.Settings;

    // Loop through the collection and
    // display the appSettings key, value pairs.
    Console.WriteLine(
        "[appSettings for app at: /configTest");
    Console.WriteLine(" and site: Default Web Site]");

    foreach (string key in appSettings.AllKeys)
    {
        Console.WriteLine("Name: {0} Value: {1}",
        key, appSettings[key].Value);
    }

    Console.WriteLine();
}
' Show how to use OpenWebConfiguration(string, string).
' It gets he appSettings section of a Web application 
' runnig on the local server. 
Shared Sub OpenWebConfiguration2()
   ' Get the configuration object for a Web application
   ' running on the local server. 
     Dim config As System.Configuration.Configuration = _
     WebConfigurationManager.OpenWebConfiguration( _
     "/configTest", "Default Web Site")
   
   ' Get the appSettings.
     Dim appSettings As KeyValueConfigurationCollection = _
     config.AppSettings.Settings
   
   
   ' Loop through the collection and
   ' display the appSettings key, value pairs.
   Console.WriteLine("[appSettings for app at: /configTest")
   Console.WriteLine(" and site: Default Web Site]")
   
   Dim key As String
   For Each key In  appSettings.AllKeys
         Console.WriteLine("Name: {0} Value: {1}", _
         key, appSettings(key).Value)
   Next key
   
   Console.WriteLine()
End Sub

注解

若要获取 Configuration 资源的 对象,代码必须对其从中继承设置的所有配置文件具有读取权限。 若要更新配置文件,代码还必须对配置文件及其所在的目录具有写入权限。

另请参阅

适用于

OpenWebConfiguration(String, String, String)

使用指定的虚拟路径、网站名称和位置将 Web 应用程序配置文件作为 Configuration 对象打开以允许读或写操作。

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

参数

path
String

配置文件的虚拟路径。

site
String

显示在 Internet 信息服务 (IIS) 配置中的应用程序网站的名称。

locationSubPath
String

配置应用的特定资源。

返回

Configuration 对象。

例外

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

示例

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


// Show how to use OpenWebConfiguration(string, string, string).
// It gets he appSettings section of a Web application 
// runnig on the local server. 
static void OpenWebConfiguration3()
{
    // Get the configuration object for a Web application
    // running on the local server. 
    System.Configuration.Configuration config =
        WebConfigurationManager.OpenWebConfiguration(
        "/configTest", "Default Web Site", null)
        as System.Configuration.Configuration;

    // Get the appSettings.
    KeyValueConfigurationCollection appSettings =
         config.AppSettings.Settings;

    // Loop through the collection and
    // display the appSettings key, value pairs.
    Console.WriteLine(
        "[appSettings for app at: /configTest");
    Console.WriteLine(" site: Default Web Site");
    Console.WriteLine(" and locationSubPath: null]");
    
    foreach (string key in appSettings.AllKeys)
    {
        Console.WriteLine("Name: {0} Value: {1}",
        key, appSettings[key].Value);
    }

    Console.WriteLine();
}
' Show how to use OpenWebConfiguration(string, string, string).
' It gets he appSettings section of a Web application 
' runnig on the local server. 
Shared Sub OpenWebConfiguration3()
   ' Get the configuration object for a Web application
   ' running on the local server. 
     Dim config As System.Configuration.Configuration = _
     WebConfigurationManager.OpenWebConfiguration( _
     "/configTest", "Default Web Site", Nothing)
   
   ' Get the appSettings.
     Dim appSettings As KeyValueConfigurationCollection = _
     config.AppSettings.Settings
   
   ' Loop through the collection and
   ' display the appSettings key, value pairs.
   Console.WriteLine("[appSettings for app at: /configTest")
   Console.WriteLine(" site: Default Web Site")
   Console.WriteLine(" and locationSubPath: null]")
   
   Dim key As String
   For Each key In  appSettings.AllKeys
         Console.WriteLine("Name: {0} Value: {1}", _
         key, appSettings(key).Value)
   Next key
   
   Console.WriteLine()
End Sub

注解

若要获取 Configuration 资源的 对象,代码必须对其从中继承设置的所有配置文件具有读取权限。 若要更新配置文件,代码还必须对配置文件及其所在的目录具有写入权限。

另请参阅

适用于

OpenWebConfiguration(String, String, String, String)

使用指定的虚拟路径、站点名称、位置和服务器将 Web 应用程序配置文件作为 Configuration 对象打开以允许读或写操作。

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

参数

path
String

配置文件的虚拟路径。

site
String

显示在 Internet 信息服务 (IIS) 配置中的应用程序网站的名称。

locationSubPath
String

配置应用的特定资源。

server
String

Web 应用程序所在的服务器的网络名称。

返回

Configuration 对象。

例外

服务器参数无效。

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

示例

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


// Show how to use OpenWebConfiguration(string, string, 
// string, string).
// It gets he appSettings section of a Web application 
// running on the specified server. 
// If the server is remote your application must have the
// required access rights to the configuration file. 
static void OpenWebConfiguration4()
{
    // Get the configuration object for a Web application
    // running on the specified server.
    // Null for the subPath signifies no subdir. 
    System.Configuration.Configuration config =
           WebConfigurationManager.OpenWebConfiguration(
            "/configTest", "Default Web Site", null, "myServer")
           as System.Configuration.Configuration;
    
    // Get the appSettings.
    KeyValueConfigurationCollection appSettings =
         config.AppSettings.Settings;

    // Loop through the collection and
    // display the appSettings key, value pairs.
    Console.WriteLine("[appSettings for Web app on server: myServer]");
    foreach (string key in appSettings.AllKeys)
    {
        Console.WriteLine("Name: {0} Value: {1}",
        key, appSettings[key].Value);
    }

    Console.WriteLine();
}
' Show how to use OpenWebConfiguration(string, string, 
' string, string).
' It gets he appSettings section of a Web application 
' running on the specified server. 
' If the server is remote your application must have the
' required access rights to the configuration file. 
Shared Sub OpenWebConfiguration4()
   ' Get the configuration object for a Web application
   ' running on the specified server.
   ' Null for the subPath signifies no subdir. 
   Dim config As System.Configuration.Configuration = WebConfigurationManager.OpenWebConfiguration("/configTest", "Default Web Site", Nothing, "myServer")
   
   ' Get the appSettings.
   Dim appSettings As KeyValueConfigurationCollection = config.AppSettings.Settings
   
   
   ' Loop through the collection and
   ' display the appSettings key, value pairs.
   Console.WriteLine("[appSettings for Web app on server: myServer]")
   Dim key As String
   For Each key In  appSettings.AllKeys
      Console.WriteLine("Name: {0} Value: {1}", key, appSettings(key).Value)
   Next key
   
   Console.WriteLine()
End Sub

注解

若要获取 Configuration 远程资源的 对象,代码必须在远程计算机上具有管理权限。

另请参阅

适用于

OpenWebConfiguration(String, String, String, String, IntPtr)

使用指定的虚拟路径、站点名称、位置、服务器和安全上下文将 Web 应用程序配置文件作为 Configuration 对象打开以允许读或写操作。

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

参数

path
String

配置文件的虚拟路径。

site
String

显示在 Internet 信息服务 (IIS) 配置中的应用程序网站的名称。

locationSubPath
String

配置应用的特定资源。

server
String

Web 应用程序所在的服务器的网络名称。

userToken
IntPtr

nativeint

要使用的帐户标记。

返回

Configuration 对象。

例外

serveruserToken 参数无效。

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

示例

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


// Show how to use OpenWebConfiguration(string, string, 
// string, string, IntPtr).
// It gets he appSettings section of a Web application 
// running on a remote server. 
// If the serve is remote your application shall have the
// requires access rights to the configuration file. 
static void OpenWebConfiguration6()
{

    IntPtr userToken = 
        System.Security.Principal.WindowsIdentity.GetCurrent().Token;
   
    string user = 
        System.Security.Principal.WindowsIdentity.GetCurrent().Name;
    
    // Get the configuration object for a Web application
    // running on a remote server.
    System.Configuration.Configuration config =
        WebConfigurationManager.OpenWebConfiguration(
        "/configTest", "Default Web Site", null, 
        "myServer", userToken) as System.Configuration.Configuration;

    // Get the appSettings.
    KeyValueConfigurationCollection appSettings =
         config.AppSettings.Settings;

    // Loop through the collection and
    // display the appSettings key, value pairs.
    Console.WriteLine(
        "[appSettings for Web app on server: myServer user: {0}]", user);
    foreach (string key in appSettings.AllKeys)
    {
        Console.WriteLine("Name: {0} Value: {1}",
        key, appSettings[key].Value);
    }

    Console.WriteLine();
}
' Show how to use OpenWebConfiguration(string, string, 
' string, string, IntPtr).
' It gets he appSettings section of a Web application 
' running on a remote server. 
' If the serve is remote your application shall have the
' requires access rights to the configuration file. 
Shared Sub OpenWebConfiguration6()
   
     Dim userToken As IntPtr = _
     System.Security.Principal.WindowsIdentity.GetCurrent().Token
   
     Dim user As String = _
     System.Security.Principal.WindowsIdentity.GetCurrent().Name
   
   ' Get the configuration object for a Web application
   ' running on a remote server.
     Dim config As System.Configuration.Configuration = _
     WebConfigurationManager.OpenWebConfiguration( _
     "/configTest", "Default Web Site", _
     Nothing, "myServer", userToken)
   
   ' Get the appSettings.
     Dim appSettings As KeyValueConfigurationCollection = _
     config.AppSettings.Settings
   
   ' Loop through the collection and
   ' display the appSettings key, value pairs.
     Console.WriteLine( _
     "[appSettings for Web app on server: myServer user: {0}]", user)
   Dim key As String
   For Each key In  appSettings.AllKeys
         Console.WriteLine("Name: {0} Value: {1}", _
         key, appSettings(key).Value)
   Next key
   
   Console.WriteLine()
End Sub

注解

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

注意

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

若要获取 Configuration 远程资源的 对象,代码必须在远程计算机上具有管理权限。

另请参阅

适用于

OpenWebConfiguration(String, String, String, String, String, String)

使用指定的虚拟路径、站点名称、位置、服务器和安全上下文将 Web 应用程序配置文件作为 Configuration 对象打开以允许读或写操作。

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

参数

path
String

配置文件的虚拟路径。

site
String

显示在 Internet 信息服务 (IIS) 配置中的应用程序网站的名称。

locationSubPath
String

配置应用的特定资源。

server
String

Web 应用程序所在的服务器的网络名称。

userName
String

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

password
String

用户名的密码。

返回

Configuration 对象。

例外

serveruserNamepassword 参数无效。

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

示例

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


// Show how to use OpenWebConfiguration(string, string, 
// string, string, string, string).
// It gets he appSettings section of a Web application 
// running on a remote server. 
// If the server is remote your application must have the
// required access rights to the configuration file. 
static void OpenWebConfiguration5()
{
    // Get the current user.
    string user =
        System.Security.Principal.WindowsIdentity.GetCurrent().Name;
    
    // Assign the actual password.
    string password = "userPassword";

    // Get the configuration object for a Web application
    // running on a remote server.
    System.Configuration.Configuration config =
        WebConfigurationManager.OpenWebConfiguration(
        "/configTest", "Default Web Site", null, "myServer",
        user, password) as System.Configuration.Configuration;

    // Get the appSettings.
    KeyValueConfigurationCollection appSettings =
         config.AppSettings.Settings;

    // Loop through the collection and
    // display the appSettings key, value pairs.
    Console.WriteLine(
        "[appSettings for Web app on server: myServer user: {0}]", user);
    foreach (string key in appSettings.AllKeys)
    {
        Console.WriteLine("Name: {0} Value: {1}",
        key, appSettings[key].Value);
    }

    Console.WriteLine();
}
' Show how to use OpenWebConfiguration(string, string, 
' string, string, string, string).
' It gets he appSettings section of a Web application 
' running on a remote server. 
' If the server is remote your application must have the
' required access rights to the configuration file. 
Shared Sub OpenWebConfiguration5()
   ' Get the current user.
     Dim user As String = _
     System.Security.Principal.WindowsIdentity.GetCurrent().Name
   
   ' Assign the actual password.
   Dim password As String = "userPassword"
   
   ' Get the configuration object for a Web application
   ' running on a remote server.
     Dim config As System.Configuration.Configuration = _
     WebConfigurationManager.OpenWebConfiguration( _
     "/configTest", "Default Web Site", _
     Nothing, "myServer", user, password)
   
   ' Get the appSettings.
     Dim appSettings As KeyValueConfigurationCollection = _
     config.AppSettings.Settings
   
   
   ' Loop through the collection and
   ' display the appSettings key, value pairs.
     Console.WriteLine( _
     "[appSettings for Web app on server: myServer user: {0}]", user)
   Dim key As String
   For Each key In  appSettings.AllKeys
         Console.WriteLine("Name: {0} Value: {1}", _
         key, appSettings(key).Value)
   Next key
   
   Console.WriteLine()
End Sub

注解

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

若要获取 Configuration 远程资源的 对象,代码必须在远程计算机上具有管理权限。

可能需要运行 ASP.NET IIS 注册工具 (Aspnet_regiis.exe) ,并 -config+ 启用对远程计算机上的配置文件的访问选项。

另请参阅

适用于