配置文件在.net应用开发中确实是个比较好的东西。有了它,咱们在开发的过程当中能够省不少事。这篇文章主要的是讲怎么读取简单的自定义配置节点。web
首先来说下基本知识。Asp.net中的配置是使用一个XML格式的配置文件来管理的。该文件能够包含应用程序须要的信息,也能够包含自定义的信息。配置文件分为机器级配置文件、应用程序级配置文件、文件夹级配置文件。机器级别的好比:C:\Windows\Microsoft.NET\Framework\v4.0.30319\Config 文件夹中的machine.config 和web.config。应用程序级和文件夹级的应该好理解,通常开发项目中用的多。编程
ASP.NET使用一种多层配置系统,容许开发人员在不一样的级别添加配置信息。在运行时,配置被应用的顺序为:应用默认的machine.config,应用来自计算机的web.config,程序根目录下的web.config,文件夹下的web.config。服务器
关于web.config中一些节点的做用我这里就不讲了。web.config文件的编程读取。在读取<connenctionStrings>和<appSettings>节点能够用System.Configuration.ConfigurationManger。但若是要修改web.config的时候,就必须用System.Web.Configuration.WebConfigurationManager。WebConfigurationManager 主要成员以下:app
Appsettings属性:提供访问添加到应用程序配置文件<appSettings>配置节点中自定义的信息。tcp
ConnectionStrings属性:提供访问配置文件中<connectionStrings>节中的数据,能够由名称索引来访问单独的连接字符串。ide
OpenWebConfiguration方法:返回指定应用程序中配置信息的Configuration对象。网站
OpenMachineConfiguration 方法:返回machine.config中配置信息的Configuration对象。ui
尽管使用<appSettings>元素能够存储自定义信息,可是该元素具备限制:该配置节点不能存储结构化的信息。spa
假定要将几个相关的设置组合在一块儿,使应用程序获知如何联系一个远程对象,好比指定一个端口号、服务器位置、URL及用户验证信息。首先建立一个网站,打开根目录下web.config文件,在<configuration>节点中添加.net
1 <RemotingObject available="true" pollTimeout="00:01:00" location="tcp://OrderComputer:8010/OrderService" ></RemotingObject>
要实现这个结构,咱们须要定义一个相匹配的类,该类派生自System.Configuration.ConfigurationSection。在网站中添加System.Configuration程序集。而后在App_Code文件夹下添加RemotingObject类。代码以下
1 using System; 2 using System.Configuration; 3 4 /// <summary> 5 /// RemotingObject 的摘要说明 6 /// </summary> 7 public class RemotingObject:ConfigurationSection 8 { 9 10 [ConfigurationProperty("available", IsRequired = false, DefaultValue = true)] 11 public bool Available 12 { 13 get { return (bool)base["available"]; } 14 set { base["available"] = value; } 15 } 16 17 [ConfigurationProperty("pollTimeout", IsRequired = true)] 18 public TimeSpan PollTimeout 19 { 20 get { return (TimeSpan)base["pollTimeout"]; } 21 set 22 { 23 base["pollTimeout"] = value; 24 } 25 } 26 27 [ConfigurationProperty("location", IsRequired = true)] 28 public string Location 29 { 30 get { return (string)base["location"]; } 31 set { base["location"] = value; } 32 } 33 }
代码中每一个属性都使用ConfigurationProperty Attribute 映射到相应的<RemotingObejct>节点的属性名称。若是自定义配置节点添加了一个属性,可是没有包含匹配的ConfigurationProperty特性的话,那么运行时有抛出一个异常。
建立完后,在web.config配置文件中的<configSections>区块中添加一个<section>,代码以下
1 <section name="RemotingObject" type="RemotingObject"/>
最后咱们就可使用代码来从自定义的配置节点中获取信息了。代码以下
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Web; 5 using System.Web.UI; 6 using System.Web.UI.WebControls; 7 using System.Configuration; 8 using System.Web.Configuration; 9 10 public partial class _Default : Page 11 { 12 protected void Page_Load(object sender, EventArgs e) 13 { 14 System.Configuration.Configuration config = WebConfigurationManager.OpenWebConfiguration("~/"); 15 RemotingObject custSection = (RemotingObject)config.GetSection("RemotingObject"); 16 Label1.Text += "获取自定义配置节点的信息...<br/>" + 17 "<b>位置</b>" + custSection.Location + 18 "<br/><b>是否可用:</b>" + custSection.Available.ToString() + 19 "<br/> <b>超时时间:</b>" + custSection.PollTimeout.ToString() + "<br/><br/>"; 20 } 21 }
注意加命名空间:using System.Web.Configuration;运行效果以下
总结:要扩展一个配置文件须要三个步骤
1.决定想要在配置文件中存储的信息及如何将这些信息组织为元素和属性。
2.对于每个新元素,建立一个C#类来封装信息。
3.向配置文件中注册新的配置块,须要使用<configSection>元素。