web.config文件自定义配置节的使用方法的一个简单例子

来源:互联网 发布:开淘宝身份证照片 编辑:程序博客网 时间:2024/05/17 11:03
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 728x15, 创建于 08-4-23MSDN */google_ad_slot = "3624277373";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 160x600, 创建于 08-4-23MSDN */google_ad_slot = "4367022601";google_ad_width = 160;google_ad_height = 600;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
web.config文件自定义配置节的使用方法一个简单例子
用来演示的程序名为MyApp,Namespace也是MyApp

1。编辑web.config文件

添加以下内容,声明一个Section

<configSections>
  <sectionname="Appconfig"type="MyApp.Appconfig,MyApp"/>
</configSections>  

声明了一个叫Appconfig的Section

2。编辑web.config文件

添加以下内容,加入一个Section

<Appconfig>
  <addkey="ConnectionString"value="thisisaConnectionString"/>
  <addkey="UserCount"value="199"/>
</Appconfig

这个Section包括两个Key

3。从IconfigurationSectionHandler派生一个类,Appconfig

实现Create方法,代码如下

publicclassAppconfig:IconfigurationSectionHandler
{
  staticStringm_connectionString=String.Empty;
  staticInt32m_userCount=0;
  publicstaticStringConnectionString
  {
  get
  {
    returnm_connectionString;
  }
  }
  publicstaticInt32UserCount
  {
  get
  {
    returnm_userCount;
  }
  }

  staticStringReadSetting(NameValueCollectionnvc,Stringkey,StringdefaultValue)
  {
  StringtheValue=nvc[key];
  if(theValue==String.Empty)
    returndefaultValue;

  returntheValue;
  }

  publicobjectCreate(objectparent,objectconfigContext,XmlNodesection)
  {
  NameValueCollectionsettings;
  
  try
  {
    NameValueSectionHandlerbaseHandler=newNameValueSectionHandler();
    settings=(NameValueCollection)baseHandler.Create(parent,configContext,section);
  }
  catch
  {
    settings=null;
  }
  
  if(settings!=null)
  {
    m_connectionString=Appconfig.ReadSetting(settings,"ConnectionString",String.Empty);
    m_userCount=Convert.ToInt32(Appconfig.ReadSetting(settings,"UserCount","0"));
  }
  
  returnsettings;
  }
}

我们把所有的配置都映射成相应的静态成员变量,并且是写成只读属性,这样程序通过

类似Appconfig.ConnectionString就可以访问,配置文件中的项目了

4。最后还要做一件事情

在Global.asax.cs中的Application_Start中添加以下代码

System.configuration.configurationSettings.Getconfig("Appconfig");

这样在程序启动后,会读取Appconfig这个Section中的值,系统会调用你自己实现的IconfigurationSectionHandler接口来读取配置

<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 728x15, 创建于 08-4-23MSDN */google_ad_slot = "3624277373";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 160x600, 创建于 08-4-23MSDN */google_ad_slot = "4367022601";google_ad_width = 160;google_ad_height = 600;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>