WebConfigurationManager 类

定义

提供对配置文件的访问权限,因为它们适用于 Web 应用程序。

public ref class WebConfigurationManager abstract sealed
public static class WebConfigurationManager
type WebConfigurationManager = class
Public Class WebConfigurationManager
继承
WebConfigurationManager

示例

以下示例演示如何使用 访问配置信息

GetSection 方法。


// Show how to use the GetSection(string).
// to access the connectionStrings section.
static void GetConnectionStringsSection()
{

    // Get the connectionStrings section.
    ConnectionStringsSection connectionStringsSection =
        WebConfigurationManager.GetSection("connectionStrings")
        as ConnectionStringsSection;

    // Get the connectionStrings key,value pairs collection.
    ConnectionStringSettingsCollection connectionStrings =
        connectionStringsSection.ConnectionStrings;

    // Get the collection enumerator.
    IEnumerator connectionStringsEnum =
        connectionStrings.GetEnumerator();

    // Loop through the collection and
    // display the connectionStrings key, value pairs.
    int i = 0;
    Console.WriteLine("[Display the connectionStrings]");
    while (connectionStringsEnum.MoveNext())
    {
        string name = connectionStrings[i].Name;
        Console.WriteLine("Name: {0} Value: {1}",
        name, connectionStrings[name]);
        i += 1;
    }

    Console.WriteLine();
}
' Show how to use the GetSection(string). 
' to access the connectionStrings section.
Shared Sub GetConnectionStringsSection()
   
   ' Get the connectionStrings section.
     Dim connectionStringsSection As ConnectionStringsSection = _
     WebConfigurationManager.GetSection("connectionStrings")
   
   ' Get the connectionStrings key,value pairs collection.
     Dim connectionStrings As ConnectionStringSettingsCollection = _
     connectionStringsSection.ConnectionStrings
   
   ' Get the collection enumerator.
     Dim connectionStringsEnum As IEnumerator = _
     connectionStrings.GetEnumerator()
   
   ' Loop through the collection and 
   ' display the connectionStrings key, value pairs.
   Dim i As Integer = 0
   Console.WriteLine("[Display the connectionStrings]")
   While connectionStringsEnum.MoveNext()
      Dim name As String = connectionStrings(i).Name
         Console.WriteLine("Name: {0} Value: {1}", _
         name, connectionStrings(name))
      i += 1
   End While
   
   Console.WriteLine()
End Sub

注解

WebConfigurationManager 类允许你访问计算机和应用程序信息。

使用 WebConfigurationManager 是处理与 Web 应用程序相关的配置文件的首选方法。 对于客户端应用程序,请使用 ConfigurationManager 类。

应用程序可以扩展 System.Configuration 类型或使用它们直接处理配置信息,如以下列表所述:

继承者说明

Configuration 类允许以编程方式访问编辑配置文件。 使用由 WebConfigurationManager其中一个提供的开放方法。 这些方法将返回一个 Configuration 对象,该对象又提供处理基础配置文件所需的方法和属性。 可按如下所示访问这些文件进行读取或写入:

使用 GetSection(String)GetSectionGroup(String) 读取配置信息。 请注意,读取的用户或进程必须具有以下权限:

  • 对当前配置层次结构级别的配置文件的读取权限。

  • 读取所有父配置文件的权限。

如果应用程序需要对其自己的配置进行只读访问权限,建议使用 GetSection 该方法。 这些方法提供对当前应用程序的缓存配置值的访问权限,其性能优于 Configuration 类。

注意:如果使用采用GetSection参数的静态path方法,路径参数必须引用运行代码的应用程序;否则,将忽略该参数,并返回当前正在运行的应用程序的配置信息。

可以使用其中 Save 一种方法来编写配置信息。 请注意,写入的用户或进程必须具有以下权限:

  • 对当前配置层次结构级别的配置文件和目录的写入权限。

  • 读取所有配置文件的权限。

属性

名称 说明
AppSettings

获取网站的应用程序设置。

ConnectionStrings

获取网站的连接字符串。

方法

名称 说明
GetSection(String, String)

从位于指定位置的 Web 应用程序的配置文件中检索指定的配置节。

GetSection(String)

从当前 Web 应用程序的配置文件中检索指定的配置节。

GetWebApplicationSection(String)

从当前 Web 应用程序的配置文件中检索指定的配置节。

OpenMachineConfiguration()

以对象的形式 Configuration 打开当前计算机上的计算机配置文件,以允许读取或写入操作。

OpenMachineConfiguration(String, String, IntPtr)

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

OpenMachineConfiguration(String, String, String, String)

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

OpenMachineConfiguration(String, String)

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

OpenMachineConfiguration(String)

以对象的形式 Configuration 打开当前计算机上的计算机配置文件,以允许读取或写入操作。

OpenMappedMachineConfiguration(ConfigurationFileMap, String)

使用指定的文件映射和位置打开计算机配置文件作为 Configuration 对象,以允许读取或写入操作。

OpenMappedMachineConfiguration(ConfigurationFileMap)

使用指定的文件映射将计算机配置文件作为 Configuration 对象打开,以允许读取或写入操作。

OpenMappedWebConfiguration(WebConfigurationFileMap, String, String, String)

使用指定的文件映射、虚拟路径、站点名称和位置打开指定的 Web 应用程序配置文件, Configuration 以允许读取或写入操作。

OpenMappedWebConfiguration(WebConfigurationFileMap, String, String)

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

OpenMappedWebConfiguration(WebConfigurationFileMap, String)

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

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

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

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

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

OpenWebConfiguration(String, String, String, String)

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

OpenWebConfiguration(String, String, String)

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

OpenWebConfiguration(String, String)

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

OpenWebConfiguration(String)

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

适用于

另请参阅