AppConfig的操作
2017-02-21 18:17:21 访问(1478) 赞(0) 踩(0)
using System;
using System.Xml;
using System.Configuration;
using System.Collections;
using System.Reflection;
using System.Diagnostics;
using CT.BA.Classes.Loger;
namespace CT.BA.Classes.Functions
{
/// <summary>
/// 配置文件类型
/// </summary>
public enum ConfigFileType
{
WebConfig,
AppConfig
}
/// <summary>
/// AppConfig的操作
/// </summary>
public class AppConfig
:
System.Configuration.AppSettingsReader
{
/// <summary>
/// 只能用保护,不然反射不到
/// </summary>
protected static AppConfig m_instance = null;
/// <summary>
/// 设置AppConfig的默认实体
/// </summary>
/// <param name="_instance"></param>
public static void SetInstance(AppConfig _instance)
{
m_instance = _instance;
}
/// <summary>
/// 获得AppConfig的实体
/// </summary>
public static AppConfig instance
{
get
{
if (m_instance == null)
{
// throw new Exception("未有系统实例化!");
m_instance = new AppConfig();
}
return m_instance;
}
}
/// <summary>
/// 配置文件的路径值
/// </summary>
public string docName = String.Empty;
/// <summary>
///
/// </summary>
private XmlNode node = null;
/// <summary>
/// 配置文件类型
/// </summary>
private int _configType;
/// <summary>
/// 配置文件类型
/// </summary>
public int ConfigType
{
get
{
return _configType;
}
set
{
_configType = value;
}
}
/// <summary>
/// 设置节点值
/// </summary>
/// <param name="key"></param>
/// <param name="value"></param>
/// <returns></returns>
public bool SetValue(string key, string value)
{
XmlDocument cfgDoc = new XmlDocument();
loadConfigDoc(cfgDoc);
// retrieve the appSettings node
node = cfgDoc.SelectSingleNode("//appSettings");
if (node == null)
{
throw new System.InvalidOperationException("appSettings section not found");
}
try
{
// XPath select setting "add" element that contains this key
XmlElement addElem = (XmlElement)node.SelectSingleNode("//add[@key='" + key + "']");
if (addElem != null)
{
addElem.SetAttribute("value", value);
}
// not found, so we need to add the element, key and value
else
{
XmlElement entry = cfgDoc.CreateElement("add");
entry.SetAttribute("key", key);
entry.SetAttribute("value", value);
node.AppendChild(entry);
}
//save it
saveConfigDoc(cfgDoc, docName);
return true;
}
catch
{
return false;
}
}
/// <summary>
/// 保存节点配置
/// </summary>
/// <param name="cfgDoc"></param>
/// <param name="cfgDocPath"></param>
public void saveConfigDoc(XmlDocument cfgDoc, string cfgDocPath)
{
try
{
XmlTextWriter writer = new XmlTextWriter(cfgDocPath, null);
writer.Formatting = Formatting.Indented;
cfgDoc.WriteTo(writer);
writer.Flush();
writer.Close();
return;
}
catch(Exception err)
{
Recorder.Record(err);
throw err;
}
}
/// <summary>
/// 移除节点
/// </summary>
/// <param name="elementKey"></param>
/// <returns></returns>
public bool removeElement(string elementKey)
{
try
{
XmlDocument cfgDoc = new XmlDocument();
loadConfigDoc(cfgDoc);
// retrieve the appSettings node
node = cfgDoc.SelectSingleNode("//appSettings");
if (node == null)
{
throw new System.InvalidOperationException("appSettings section not found");
}
// XPath select setting "add" element that contains this key to remove
node.RemoveChild(node.SelectSingleNode("//add[@key='" + elementKey + "']"));
saveConfigDoc(cfgDoc, docName);
return true;
}
catch(Exception err)
{
throw err;
}
}
/// <summary>
/// 加载节点
/// </summary>
/// <param name="cfgDoc"></param>
/// <returns></returns>
public XmlDocument loadConfigDoc(XmlDocument cfgDoc)
{
// load the config file
if (Convert.ToInt32(ConfigType) == Convert.ToInt32(ConfigFileType.AppConfig))
{
docName = ((Assembly.GetEntryAssembly()).GetName()).Name;
docName += ".exe.config";
}
else
{
docName = System.Web.HttpContext.Current.Server.MapPath("web.config");
}
cfgDoc.Load(docName);
return cfgDoc;
}
}
}
标签:
AppConfig的操作 


上一条:
下一条:
相关评论
发表评论