WebConfigurationManager 类

来源:互联网 发布:ipad和手机同时上淘宝 编辑:程序博客网 时间:2024/05/17 06:25

在配置文件应用于 Web 应用程序时提供对配置文件的访问。

继承层次结构

System.Object 
  System.Web.Configuration.WebConfigurationManager
命名空间:  System.Web.Configuration

程序集:  System.Web(在 System.Web.dll 中)

WebConfigurationManager 类型公开以下成员。

属性

名称说明AppSettings获取该网站的应用程序设置。ConnectionStrings获取网站的连接字符串。页首
方法

名称说明GetSection(String)从当前 Web 应用程序的配置文件中检索指定的配置节。GetSection(String, String)从指定的 Web 应用程序的配置文件中的指定位置检索指定的配置节。GetWebApplicationSection从当前 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对象打开以允许读或写操作。页首
备注

使用 WebConfigurationManager 类可以访问计算机和应用程序信息。

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

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

  • Handling configuration . 若要使用标准类型处理配置信息,请使用下列方法之一:

    • Accessing a section . 若要访问应用程序的配置信息,必须使用 WebConfigurationManager 提供的 GetSection 方法之一。 对于 <appSettings>  <connectionStrings>,可以使用 AppSettings  ConnectionStrings 属性。 上面提到的方法执行只读操作,使用配置的单个缓存实例,并且可识别多线程。

    • Accessing configuration files . 应用程序能够读写任何级别的配置设置,不管是自己的还是其他应用程序或计算机的,也不管是本地的还是远程的。 可以使用WebConfigurationManager 提供的 open 方法之一。 这些方法将返回 Configuration 对象,该对象又提供处理基础配置文件所需的方法和属性。 这些方法执行读取或写入操作,而且每次打开文件时都会重新创建配置数据。

    • Advanced configuration . 以下类型提供了更高级的配置处理方法:SectionInformationPropertyInformationPropertyInformationCollectionElementInformationContextInformationConfigurationSectionGroup ConfigurationSectionGroupCollection

  • Extending configuration standard types . 通过使用编程模型或特性化模型扩展标准配置类型(如 ConfigurationElementConfigurationElementCollectionConfigurationProperty ConfigurationSection),您还可以提供自定义的配置元素。 有关如何以编程方式扩展标准的配置类型的示例,请参考 ConfigurationSection 类。 有关如何以特性化模型扩展标准的配置类型的示例,请参考 ConfigurationElement 类。

对实现者的说明

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

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

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

  • 对所有父级配置文件进行读取的权限。

如果您的应用程序需要对其自身的配置进行只读访问,则建议您使用 GetSection 方法。 这些方法提供对当前应用程序的缓存配置值的访问;与 Configuration 类相比,这样做可以提供更好的性能。

说明说明

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

使用 Save 方法之一写入配置信息。 请注意,进行写入操作的用户或进程必须具有以下权限:

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

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

TopicLocation如何:以编程方式访问 ASP.NET 配置设置配置 ASP .NET Web 应用程序如何:锁定 ASP.NET 配置设置配置 ASP .NET Web 应用程序如何:以编程方式查看继承的配置设置和本地配置设置配置 ASP .NET Web 应用程序如何:以编程方式访问 ASP.NET 配置设置在 Visual Studio 中生成 ASP .NET Web 应用程序如何:锁定 ASP.NET 配置设置在 Visual Studio 中生成 ASP .NET Web 应用程序如何:以编程方式查看继承的配置设置和本地配置设置在 Visual Studio 中生成 ASP .NET Web 应用程序
示例

下面的示例演示如何使用 访问配置信息。

GetSection 方法。

C#
VB
        // 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();        }
版本信息

.NET Framework

受以下版本支持:4.5、4、3.5、3.0、2.0

0 0