/// <summary>
/// 设置程序配置文件
/// </summary>
/// <param name="keyWord">配置关键词</param>
public static string GetAppSettings(string pKeyWord)
{
string pKeyValue = "";
try
{
XmlDocument pXmlDoc = new XmlDocument();
pXmlDoc.Load(m_ConfigFilePath);
XmlNode pRoot = pXmlDoc.DocumentElement.SelectSingleNode("appSettings");//查找<appSettings>
XmlNodeList pNodeList = pRoot.ChildNodes;
foreach (XmlNode xNode in pNodeList)//遍历所有子节点
{
if (xNode.NodeType == XmlNodeType.Element)
{
XmlElement xElement = (XmlElement)xNode;//将子节点类型转换为XmlElement类型
if (xElement.GetAttribute("key") == pKeyWord)//如果key属性值为key
{
pKeyValue = xElement.GetAttribute("value");
break;
}
}
}
pXmlDoc = null;
pRoot = null;
pNodeList = null;
}
catch (Exception)
{
//LogInfo.AppendLog(exp);
pKeyValue = "";
}
return pKeyValue;
}
/// <summary>
/// 设置程序配置文件
/// </summary>
/// <param name="keyWord">配置关键词</param>
/// <param name="keyValue">配置值</param>
public static void SetAppSettings(string pKeyWord, string pKeyValue)
{
try
{
XmlDocument pXmlDoc = new XmlDocument();
pXmlDoc.Load(m_ConfigFilePath);
XmlNode pRoot = pXmlDoc.DocumentElement.SelectSingleNode("appSettings");//查找<appSettings>
XmlNodeList pNodeList = pRoot.ChildNodes;
foreach (XmlNode xNode in pNodeList)//遍历所有子节点
{
if (xNode.NodeType == XmlNodeType.Element)
{
XmlElement xElement = (XmlElement)xNode;//将子节点类型转换为XmlElement类型
if (xElement.GetAttribute("key") == pKeyWord)//如果key属性值为key
{
xElement.SetAttribute("value", pKeyValue);
break;
}
}
}
pXmlDoc.Save(m_ConfigFilePath);//保存
pXmlDoc = null;
pRoot = null;
pNodeList = null;
}
catch (Exception)
{
//LogInfo.AppendLog(exp);
}
}
/// <summary>
/// 添加指定节点的配置信息
/// </summary>
/// <param name="keyWord">配置关键词</param>
/// <param name="keyValue">配置值</param>
public static void AddAppSettings(string pKeyWord, string pKeyValue)
{
try
{
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(m_ConfigFilePath);
XmlNode rootNode = xmlDoc.DocumentElement.SelectSingleNode("appSettings");//查找<appSettings>
XmlNodeList xmlNodeList = rootNode.ChildNodes;
foreach (XmlNode xNode in xmlNodeList)
{
XmlElement xmlElement = (XmlElement)xNode;
if (xmlElement.GetAttribute("key") != pKeyWord )
{
xmlElement.SetAttribute("key", pKeyWord);//设置该节点key属性
xmlElement.SetAttribute("value", pKeyValue);//设置该节点value属性
rootNode.AppendChild(xmlElement);
}
else
{
break;
}
}
xmlDoc.Save(m_ConfigFilePath);
}
catch (Exception ex)
{
//throw new Exception(ex.Message);
}
}
/// <summary>
/// 删除指定节点的配置信息
/// </summary>
/// <param name="keyWord">配置关键词</param>
public static void DelAppSettings(string pKeyWord)
{
try
{
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(m_ConfigFilePath);
XmlNode rootNode = xmlDoc.DocumentElement.SelectSingleNode("appSettings");//查找<appSettings>
XmlNodeList xNodeList = rootNode.ChildNodes;
foreach (XmlNode xNode in xNodeList)
{
XmlElement xmlElement = (XmlElement)xNode;
if (xmlElement.GetAttribute("key") == pKeyWord)
{
xmlElement.RemoveAll();//删除该节点的全部内容
}
}
xmlDoc.Save(m_ConfigFilePath);
}
catch (Exception ex)
{
//throw new Exception(ex.Message);
}
}
}