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 类。

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

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

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

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

属性

AppSettings

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

ConnectionStrings

获取网站的连接字符串。

方法

GetSection(String)

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

GetSection(String, String)

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

GetWebApplicationSection(String)

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

OpenMachineConfiguration()

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

OpenMachineConfiguration(String)

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

OpenMachineConfiguration(String, String)

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

OpenMachineConfiguration(String, String, IntPtr)

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

OpenMachineConfiguration(String, String, String, String)

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

OpenMappedMachineConfiguration(ConfigurationFileMap)

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

OpenMappedMachineConfiguration(ConfigurationFileMap, String)

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

OpenMappedWebConfiguration(WebConfigurationFileMap, String)

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

OpenMappedWebConfiguration(WebConfigurationFileMap, String, String)

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

OpenMappedWebConfiguration(WebConfigurationFileMap, String, String, String)

使用指定的文件映射、虚拟路径、站点名称和位置将指定的 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 对象打开以允许读或写操作。

适用于

另请参阅