明輝手游網(wǎng)中心:是一個(gè)免費(fèi)提供流行視頻軟件教程、在線學(xué)習(xí)分享的學(xué)習(xí)平臺(tái)!

取得設(shè)置文件中configSections的設(shè)置信息!

[摘要]configSections的作用很多!我就不多舉了。首先要注意的一點(diǎn)是!configSections關(guān)聯(lián)的類型在一個(gè)進(jìn)程內(nèi)只實(shí)例化一次,1、配置文件Web.config<?xml version="1.0" encoding="utf-8" ?>...
configSections的作用很多!我就不多舉了。

首先要注意的一點(diǎn)是!configSections關(guān)聯(lián)的類型在一個(gè)進(jìn)程內(nèi)只實(shí)例化一次,

1、配置文件Web.config

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="xinyulou" type="Config.ConfigurationHandler, Config" />
</configSections>

<xinyulou>
<add name="Region" value = "心雨樓" />
</xinyulou>


<system.web>
<compilation defaultLanguage="c#" debug="true" />
<customErrors mode="RemoteOnly" />
<authentication mode="Windows" />
<authorization>
<allow users="*" /> <!-- <sectionGroup name="xinyulou"> 允許所有用戶 -->
</authorization>
<trace enabled="false" requestLimit="10" pageOutput="false" traceMode="SortByTime" localOnly="true" />
<sessionState mode="InProc" stateConnectionString="tcpip=127.0.0.1:42424" sqlConnectionString="data source=127.0.0.1;Trusted_Connection=yes"
cookieless="false" timeout="20" />
<globalization requestEncoding="utf-8" responseEncoding="utf-8" />
</system.web>

</configuration>

2、兩個(gè)重要的類

using System;
using System.Configuration;
using System.Collections;
using System.Xml;

namespace Config
{
public class Configuration
{
private Hashtable m_MyRegionConfig = new Hashtable();
public Hashtable MyRegionConfig
{
get {return m_MyRegionConfig;}

}
public static Configuration GetConfig()
{
return (Configuration) ConfigurationSettings.GetConfig("xinyulou");
}
internal void LoadValuesFromConfigurationXml(XmlNode node)
{

foreach (XmlNode child in node.ChildNodes)
{
m_MyRegionConfig.Add(child.Attributes["name"].Value,child.Attributes["value"].Value);
}

}

}

internal class ConfigurationHandler : IConfigurationSectionHandler
{

public virtual object Create(Object parent, Object context, XmlNode node)
{
Configuration config = new Configuration();
config.LoadValuesFromConfigurationXml(node);
return config;
}

}
}

3、示例在aspx中用!

private void Page_Load(object sender, System.EventArgs e)
{
Response.Write(Configuration.GetConfig().MyRegionConfig["Region"].ToString());
}



configSections的威力很強(qiáng)大。有很多種用法與應(yīng)用!這里就不再舉例了,有問題可與我聯(lián)系;蛟诨謴(fù)中提出來!