通過Cache機制實現一個通用的配置管理模塊
設計目標
大致設計思路
代碼參考
CacheHelper
using System;
using System
using System
using System
using System
namespace Epower
{
/// <summary>
/// ConfigHelper 的摘要說明
/// 自定義的系統參數配置文件的讀取工具類
/// </summary>
public class ConfigHelper
{
/// <summary>
/// 取~/Config/CommonConfig
/// </summary>
/// <param name=
/// <returns>參數值</returns>
public static string GetParameterValue(string ParameterName)
{
return GetParameterValue(
}
/// <summary>
/// 取某個參數配置文件中某個參數名對應的參數值
/// 參數配置文件
///
///
/// </summary>
/// <param name=
/// <param name=
/// <returns>參數值</returns>
public static string GetParameterValue(string ConfigName
{
Hashtable CommonConfig = GetConfigCache(ConfigName);
if (CommonConfig
return CommonConfig[ParameterName]
else
throw new Exception(
}
/// <summary>
/// 將配置的參數轉換成Hashtable並存入緩存
/// </summary>
/// <param name=
/// <returns></returns>
private static Hashtable GetConfigCache(string ConfigName)
{
string CacheName =
Hashtable CommonConfig = new Hashtable();
Cache cache = HttpRuntime
if (cache[CacheName] == null)
{
string ConfigPath = null;
#region 取應用程序根物理路徑
try
{
ConfigPath = HttpRuntime
}
catch (System
{
}
if (ConfigPath == null) throw new Exception(
#endregion
string ConfigFile = ConfigPath +
XmlDocument xmlDoc = new XmlDocument();
xmlDoc
XmlNode oNode = xmlDoc
if (oNode
{
XmlNodeList oList = oNode
for (int i =
{
CommonConfig
}
}
cache
}
else
CommonConfig = (Hashtable) cache[CacheName];
return CommonConfig;
}
}
}
代碼參考
frmConfigSet
HtmlTable tab = new HtmlTable();
tab
tab
tab
HtmlTableRow tr = new HtmlTableRow();
HtmlTableCell tc = new HtmlTableCell();
XmlNodeList nodes = xmldoc
string sConfigContent =
if (xmldoc
sConfigContent = xmldoc
string sItemDesc =
int iRow =
foreach (XmlNode node in nodes)
{
iRow++;
tr = new HtmlTableRow();
tr
AddControlByNode(node
AddControl(tab
sItemDesc =
if (node
sItemDesc = node
if (sItemDesc
{
//添加描述行
iRow++;
tr = new HtmlTableRow();
tr
tc = new HtmlTableCell();
tc
tc
tc
tc
AddControl(tr
AddControl(tab
}
}
/// <summary>
/// 獲取設置的控件
/// </summary>
/// <param name=
/// <returns></returns>
private Control GetSettingControl(XmlNode node)
{
string strCtrlType = node
string strValue = node
Control control;
switch (strCtrlType
{
case
control = new TextBox();
control
((TextBox)control)
((TextBox)control)
((TextBox)control)
break;
case
control = new CheckBox();
control
((CheckBox)control)
((CheckBox)control)
if (strValue
{
((CheckBox)control)
}
else
{
((CheckBox)control)
}
break;
case
control = new DropDownList();
control
((DropDownList)control)
((DropDownList)control)
string[] sItems = node
for (int i =
{
string[] arr = sItems[i]
((DropDownList)control)
}
((DropDownList)control)
break;
case
control = (Epower
((Epower
control
((Epower
break;
default:
control = null;
break;
}
return control;
}
配置文件范例
讀取配置信息范例
string sSmtpServer = ConfigHelper
<?xml version=
<EmailConfig Title=
<Item Name=
</Item>
<Item Name=
</Item>
<Item Name=
</Item>
<Item Name=
</Item>
<Item Name=
</Item>
<Item Name=
</Item>
<Item Name=
</Item>
<Item Name=
</Item>
</EmailConfig>
From:http://tw.wingwit.com/Article/program/net/201311/13658.html