ASP.NET Lab

The Best Web, The Best Future

博客园 首页 新随笔 订阅 管理

因为每个 ASP.NET 应用程序都从根 Web.config 文件中继承默认的配置设定,你只有在需要对默认的设定进行重载的时候才需要创建新的 Web.config 文件。如果配置层次中没有其他的 Web.config 文件,你可能并不知道你的应用程序已经继承了默认的配置设定,因此你也可能不知道需要对什么内容进行重载。

本实例使用了非静态的方法来获取配置数据,并允许你获取任何应用程序中的配置信息。如果你准备从代码所在的应用程序中获取配置信息,请使用静态的方法,以获得更快的执行速度。

实例

下例代码获取了应用程序 MyApp 中的所有配置设定(该应用程序位于默认网站中),并且还把设定写进了 XML 文件。

using System;
using System.Collections.Generic;
using System.Text;
using System.Configuration;
using System.Web.Configuration;

namespace SamplesAspNet.Config
{
    class GetFullConfig
    {
        public static void Main(string[] args)
        {
            Configuration config = WebConfigurationManager.OpenWebConfiguration("/MyApp");
            config.SaveAs("c:\\MyApp.web.config", ConfigurationSaveMode.Full, true);
        }
    }
}

编译代码

  • 本例代码需要引用到 System.Web.ConfigurationSystem.Configuration 名字空间。

posted on 2006-12-22 20:39  Laeb  阅读(236)  评论(0编辑  收藏  举报