C#中动态读写App.config配置文件
日期:2007-11-29 18:14:59 作者:山人 人气: 来源:网络
--------------------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Text;
using
using System.Xml;
namespace
{
///
/// C#中动态读写App.config配置文件
///
public class AppConfig
{
public AppConfig()
{
///
/// TODO: 在此处添加构造函数逻辑
///
}
///
/// 写操作
///
///
///
///
public static void ConfigSetValue(string strExecutablePath,string AppKey, string AppValue)
{
XmlDocument xDoc = new XmlDocument();
//获取可执行文件的路径和名称
xDoc.Load(strExecutablePath + ".config");
XmlNode xNode;
XmlElement xElem1;
XmlElement xElem2;
xNode = xDoc.SelectSingleNode("//connectionStrings");
// xDoc.Load(System.Windows.Forms.Application.ExecutablePath + ".config");
xElem1 = (XmlElement)xNode.SelectSingleNode("//add[@name='" + AppKey + "']");
if (xElem1 != null) xElem1.SetAttribute("connectionString", AppValue);
else
{
xElem2 = xDoc.CreateElement("add");
xElem2.SetAttribute("name", AppKey);
xElem2.SetAttribute("connectionString", AppValue);
xNode.AppendChild(xElem2);
}
xDoc.Save(strExecutablePath + ".config");
}
///
/// 读操作
///
///
///
///
public string ConfigGetValue(string strExecutablePath, string appKey)
{
XmlDocument xDoc = new XmlDocument();
try
{
xDoc.Load(strExecutablePath + ".config");
XmlNode xNode;
XmlElement xElem;
xNode = xDoc.SelectSingleNode("//appSettings");
xElem = (XmlElement)xNode.SelectSingleNode("//add[@key='" + appKey + "']");
if (xElem != null)
return xElem.GetAttribute("value");
else
return "";
}
catch (Exception)
{
return "";
}
}
}
}
这是我找到的资料,为什么不行呢,调用时找不到ConfigGetValue这个函数。
如果行的话,如何调用,参数怎么写?