C#中动态读写App.config配置文件,调用方法的参数应该填什么?
我想调用ConfigSetValue()和ConfigGetValue(),但是参数应该怎么写?//////C#中动态读写App.config配置文件///publicc...
我想调用ConfigSetValue()和ConfigGetValue(),但是参数应该怎么写?
///
/// 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 "";
}
}
} 展开
///
/// 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 "";
}
}
} 展开
展开全部
先说写的public static void ConfigSetValue(string strExecutablePath,string AppKey, string AppValue)//strExecutablePath为配置文件的路径(去除后缀);AppKey为配置文件的节点名;AppValue为值
再说读public string ConfigGetValue(string strExecutablePath, string appKey)//strExecutablePath同上;appKey同上
再说读public string ConfigGetValue(string strExecutablePath, string appKey)//strExecutablePath同上;appKey同上
更多追问追答
追问
就是不知道路径怎么获取,请大侠赐教
追答
你的配置文件是跟Bin的Debug文件下吗
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询