Share via


使用配置类

更新:2007 年 11 月

大多数的配置工作是通过 Configuration 类完成的。此类表示计算机的配置、.NET 客户端应用程序的配置、ASP.NET 应用程序的配置、Web 目录的配置和存储在 Web 目录中的资源的配置。

在 ASP.NET 2.0 版中,通过使用 WebConfigurationManager 对象的方法,可以获得对 Configuration 类的实例的访问,以获取配置节。(在 .NET Framework 客户端应用程序中,可以使用类似的 ConfigurationManager 对象)。每个配置节具有自己的对象类型,作为节处理程序并列于 ASP.NET 配置设置中的参考主题中的“元素信息”表中。有关示例,请参见如何:以编程方式访问 ASP.NET 配置设置

下面几节描述如何在不同的方案中使用配置类。有关配置类的摘要,请参见 ASP.NET 配置 API 概述

以下所有示例通过首先创建 System.Configuration.Configuration 类的实例,使用非静态的 Configuration.GetSectionConfiguration.GetSectionGroup 方法。这允许您从任何应用程序获取配置信息。如果要从代码驻留的应用程序中获取配置信息,请使用处理速度更快的静态 GetSection 方法。有关更多信息,请参见 ASP.NET 配置 API 概述中的“使用本地和远程配置设置”节。

打开映射到全局配置设置的配置对象

若要为全局配置打开配置文件,应用程序会调用 WebConfigurationManager 类的 OpenMachineConfiguration 静态方法。在下面的代码示例中,OpenMachineConfiguration 方法打开并返回配置对象,该配置对象对应于 .NET Framework 2.0 的 Machine.config 文件。此重载方法的其他版本允许指定位置、远程服务器或用户信息。

' Obtains the machine configuration settings on the local machine.
Dim machineConfig As System.Configuration.Configuration
machineConfig = _
    System.Web.Configuration.WebConfigurationManager.OpenMachineConfiguration()
machineConfig.SaveAs("c:\machineConfig.xml")
         // Obtains the machine configuration settings on the local machine.
            System.Configuration.Configuration machineConfig = 
                System.Web.Configuration.WebConfigurationManager.OpenMachineConfiguration();
            machineConfig.SaveAs("c:\\machineConfig.xml");

配置 API 是特定于版本的。因此,不能通过使用包括在 .NET Framework 一个版本中的方法,以打开 .NET Framework 另一个版本的 Machine.config 文件。

所需特权

要打开该计算机配置文件,应用程序需要对物理 Machine.config 文件的读权限。若要修改计算机配置,应用程序需要该文件的写权限,及需要对 .NET Framework 配置目录的创建特权。

打开映射到 Web 应用程序配置设置的配置对象

若要打开 Web 应用程序的配置文件,应用程序调用 WebConfigurationManager 类的 OpenWebConfiguration 静态方法,传递要打开的 Internet 信息服务 (IIS) 虚拟目录的相对路径。

路径参数的值可以从包含所需配置的目录的 IIS 元数据库路径获取。例如,如果 IIS 元数据库路径为 W3SVC/1/Root/Temp,则路径为 /Temp,因为默认站点为 1。

在下面的代码示例中,OpenWebConfiguration 方法打开并返回配置对象,该配置对象对应于默认网站中的 Temp Web 应用程序。配置对象包含在 Web.config 文件中本地指定的配置设置以及所有从父配置文件中继承的设置(继承一直上溯至 Machine.config 文件)。此重载方法的其他版本允许指定网站、远程服务器和用户信息。

' Obtains the configuration settings for a Web application.
Dim webConfig As System.Configuration.Configuration
webConfig = _
    System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration("/Temp")
webConfig.SaveAs("c:\\webConfig.xml")
         // Obtains the configuration settings for a Web application.
            System.Configuration.Configuration webConfig =
                System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration("/Temp");
            webConfig.SaveAs("c:\\webConfig.xml");

即使物理的 Web.config 文件不存在,也可以打开 System.Configuration.Configuration 对象。这种情况下,返回的配置仅包含从配置文件层次结构中继承的全部设置。有关更多信息,请参见 ASP.NET 配置方案

所需特权

若要打开 Web 配置文件,应用程序需要对物理 Web.config 文件以及层次结构中该文件的所有父文件具有读权限。

打开映射到配置文件中的节的配置对象

若要从节中获取配置信息,应用程序调用 Configuration 类的 GetSectionGroup 非静态方法,并传递节的名称。对于包含在节组中的节,可以使用节的 XPath (system.web/anonymousIdentification),或者可以先获取映射到节组的配置对象。有关节名称的列表,请参见 ASP.NET 配置设置

在下面的代码示例中,OpenWebConfiguration 方法打开并返回一个配置对象,对应于默认网站中的 Temp Web 应用程序,然后使用该对象获取对 system.web 节组的引用,该节组然后用以获取对 anonymousIdentification 节的引用。

' Obtains the configuration settings for the <anonymousIdentification> section.
Dim config As System.Configuration.Configuration
config = _
    System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration("/Temp")
Dim systemWeb As System.Web.Configuration.SystemWebSectionGroup
systemWeb = config.GetSectionGroup("system.web")
Dim sectionConfig As System.Web.Configuration.AnonymousIdentificationSection
sectionConfig = systemWeb.AnonymousIdentification
Dim sb As New StringBuilder
sb.AppendLine("<anonymousIdentification> attributes:")
Dim props As System.Configuration.PropertyInformationCollection
props = sectionConfig.ElementInformation.Properties
For Each prop As System.Configuration.PropertyInformation In props
    sb.Append(prop.Name.ToString())
    sb.Append(" = ")
    sb.AppendLine(prop.Value.ToString())
Next
Console.WriteLine(sb.ToString())
         // Obtains the configuration settings for the <anonymousIdentification> section.
            System.Configuration.Configuration config =
                System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration("/Temp");
            System.Web.Configuration.SystemWebSectionGroup systemWeb =
                config.GetSectionGroup("system.web") 
                as System.Web.Configuration.SystemWebSectionGroup;
            System.Web.Configuration.AnonymousIdentificationSection sectionConfig =
                systemWeb.AnonymousIdentification;
            if (null != sectionConfig)
            {
                StringBuilder sb = new StringBuilder();
                sb.Append("<anonymousIdentification> attributes:\r\n");
                System.Configuration.PropertyInformationCollection props =
                    sectionConfig.ElementInformation.Properties;
                foreach (System.Configuration.PropertyInformation prop in props)
                {
                    sb.AppendFormat("{0} = {1}\r\n", prop.Name.ToString(), prop.Value.ToString());
                }
                Console.WriteLine(sb.ToString());
            }

即使物理的 Web.config 文件不存在,也可以打开 System.Configuration.Configuration 对象。这种情况下,返回的配置仅包含从配置文件层次结构中继承的全部设置。有关更多信息,请参见 ASP.NET 配置方案

所需特权

若要打开 Web 配置文件中的一个节,应用程序需要对物理 Web.config 文件以及层次结构中该文件的所有父文件具有读权限。托管代码应用程序需要具有读取系统节的权限。默认情况下,完全信任和高信任应用程序具有这些权限。换言之,默认情况下,中等和较低信任的应用程序将不能读取配置节。

打开映射到远程配置设置的配置对象

可以使用配置 API 打开远程计算机的配置。该 API 可以打开另一台计算机的计算机配置文件,或任何 IIS 应用程序或其子目录的应用程序配置文件。

若要打开另一台计算机上的计算机配置文件, 应用程序调用 OpenMachineConfiguration 静态方法,并传递服务器名。

若要打开另一台计算机上的 Web.config 文件,应用程序调用 OpenWebConfiguration 静态方法,并传递配置对象的站点相对路径、站点标识符和服务器名。返回的配置对象包含在 Web.config 文件中本地指定的配置设置以及从父配置文件中继承的所有设置(继承一直上溯至 Machine.config 文件)。

服务器名是有效的 Windows 网络计算机名。这些名称不经 ASP.NET 特殊处理,而是直接传递给操作系统。

下面的代码示例演示如何打开计算机配置、默认站点上的根配置以及应用程序目录中的 Web 配置。

Dim userToken As IntPtr
userToken = System.Security.Principal.WindowsIdentity.GetCurrent().Token

' Obtains the machine configuration settings on a remote machine.
Dim remoteMachineConfig As System.Configuration.Configuration
remoteMachineConfig = _
    System.Web.Configuration.WebConfigurationManager.OpenMachineConfiguration( _
    Nothing, "JanetFi2", userToken)
remoteMachineConfig.SaveAs("c:\remoteMachineConfig.xml")

' Obtains the root Web configuration settings on a remote machine.
Dim remoteRootConfig As System.Configuration.Configuration
remoteRootConfig = _
    System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration( _
    Nothing, Nothing, Nothing, "JanetFi2", userToken)
remoteRootConfig.SaveAs("c:\remoteRootConfig.xml")

' Obtains the configuration settings for the 
' W3SVC/1/Root/Temp application on a remote machine.
Dim remoteWebConfig As System.Configuration.Configuration
remoteWebConfig = _
    System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration( _
    "/Temp", "1", Nothing, "JanetFi2", userToken)
remoteWebConfig.SaveAs("c:\\remoteWebConfig.xml")
         IntPtr userToken = System.Security.Principal.WindowsIdentity.GetCurrent().Token;

            // Obtains the machine configuration settings on a remote machine.
            System.Configuration.Configuration remoteMachineConfig =
                System.Web.Configuration.WebConfigurationManager.OpenMachineConfiguration
                (null, "ServerName", userToken);
            remoteMachineConfig.SaveAs("c:\\remoteMachineConfig.xml");

            // Obtains the root Web configuration settings on a remote machine.
            System.Configuration.Configuration remoteRootConfig =
                System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration
                (null, null, null, "ServerName", userToken);
            remoteRootConfig.SaveAs("c:\\remoteRootConfig.xml");

            // Obtains the configuration settings for the 
            // W3SVC/1/Root/Temp application on a remote machine.
            System.Configuration.Configuration remoteWebConfig =
                System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration
                ("/Temp", "1", null, "ServerName", userToken);
            remoteWebConfig.SaveAs("c:\\remoteWebConfig.xml");

所需特权

若要打开远程计算机上的配置文件,应用程序必须具有远程计算机的管理员权限。此要求的限制性强于本地使用的要求。对于本地配置文件,应用程序只需要对层次结构中的配置文件和 IIS 元数据库(以解析 IIS 路径)具有读权限。

在试图配置远程计算机前,请对远程计算机运行带有“-config+”参数的 Aspnet_regiis.exe 工具。有关更多信息,请参见 ASP.NET IIS 注册工具 (Aspnet_regiis.exe)

更新配置设置

若要更新打开的配置对象,应用程序调用配置对象的 SaveSaveAs 方法。这些方法可为该对象写入任何配置设置的更改。如果配置文件不存在于相应位置,则会创建新的配置文件。

下面的代码示例将 compilation 节的 debug 属性设置为 true。

' Updates the configuration settings for a Web application.
Dim updateWebConfig As System.Configuration.Configuration
updateWebConfig = _
    System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration("/Temp")
Dim compilation As System.Web.Configuration.CompilationSection
compilation = _
    updateWebConfig.GetSection("system.web/compilation")
Console.WriteLine("Current <compilation> debug = {0}", compilation.Debug)
compilation.Debug = True
If Not compilation.SectionInformation.IsLocked Then
    updateWebConfig.Save()
    Console.WriteLine("New <compilation> debug = {0}", compilation.Debug)
Else
    Console.WriteLine("Could not save configuration.")
End If
         // Updates the configuration settings for a Web application.
            System.Configuration.Configuration updateWebConfig =
                System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration("/Temp");
            System.Web.Configuration.CompilationSection compilation =
                updateWebConfig.GetSection("system.web/compilation") 
                as System.Web.Configuration.CompilationSection;
            Console.WriteLine("Current <compilation> debug = {0}", compilation.Debug);
            compilation.Debug = true;
            if (!compilation.SectionInformation.IsLocked)
            {
                updateWebConfig.Save();
                Console.WriteLine("New <compilation> debug = {0}", compilation.Debug);
            }
            else
            {
                Console.WriteLine("Could not save configuration.");
            }

当前,没有功能可用于在更新配置数据时将其锁定。因此,配置 API 使用开放式并发模型修改配置。在开放式并发模型下,如果两个应用程序同时打开相同的配置,则两个应用程序都将获得配置对象的唯一副本。如果应用程序试图通过调用 SaveSaveAs 方法来修改配置,且在应用程序获取配置对象后基础配置文件已被修改,则方法将引发异常。正在更新配置的另一个应用程序或者与配置 API 无关的其他一些对文件的更改可能已经修改了基础文件。

所需特权

若要修改 Web 配置,应用程序需要该文件的写权限,及需要对包含该文件的目录具有创建特权。

请参见

概念

ASP.NET 配置 API 概述

其他资源

ASP.NET 配置设置