本篇来讲讲对web.config文件的读写,并不是谁都可以对web.config文件进行读写操作的,必须要有一定的权限才行的。
其实在web.config中的很多的配置节点都是与一个类相对应的,我们可以通过相应的类来,利用其强类型的API来对相应的配置进行访问,而不用把web.config 当做一个XML来读取。
我们可以用两种方式来对配置文件进行访问,首先,可以用强类型的API来访问,这就要用要ConfigurationManager类。如下:
using System.Web.Configuration;
using System.Configuration;
protected void Page_Load(object sender, EventArgs e)
{
SessionStateSection sts =(SessionStateSection)ConfigurationManager.GetSection("system.web/sessionState");
Response.Write("The session state mode is: " + sts.Mode.ToString() + "<br/>");
}
using System.Configuration;
protected void Page_Load(object sender, EventArgs e)
{
SessionStateSection sts =(SessionStateSection)ConfigurationManager.GetSection("system.web/sessionState");
Response.Write("The session state mode is: " + sts.Mode.ToString() + "<br/>");
}