您的位置:首页 > 移动开发

Winform读写App.config文件以及重启程序

2016-08-07 05:28 447 查看
//重启主程序

//System.Diagnostics.Process.Start(System.Reflection.Assembly.GetExecutingAssembly().Location);

#region 读存app.config字段值

public static string GetConfigValue(string appKey)

{

XmlDocument xDoc = new XmlDocument();

try

{

//缓存路径

xDoc.Load(System.Windows.Forms.Application.ExecutablePath + ".config");

System.Xml.XmlNode xNode;

System.Xml.XmlElement xElem;

xNode = xDoc.SelectSingleNode("//appSettings");

xElem = (System.Xml.XmlElement)xNode.SelectSingleNode("//add[@key='" + appKey + "']");

if (xElem != null)

return xElem.GetAttribute("value");

else

return "";

}

catch

{

return "";

}

}

public static void SetConfigValue(string AppKey, string AppValue)

{

XmlDocument xDoc = new XmlDocument();

xDoc.Load(System.Windows.Forms.Application.ExecutablePath + ".config");

XmlNode xNode;

XmlElement xElem1;

XmlElement xElem2;

xNode = xDoc.SelectSingleNode("//appSettings");

xElem1 = (XmlElement)xNode.SelectSingleNode("//add[@key='" + AppKey + "']");

if (xElem1 != null) xElem1.SetAttribute("value", AppValue);

else

{

xElem2 = xDoc.CreateElement("add");

xElem2.SetAttribute("key", AppKey);

xElem2.SetAttribute("value", AppValue);

xNode.AppendChild(xElem2);

}

xDoc.Save(System.Windows.Forms.Application.ExecutablePath + ".config");

}

#endregion

//重启主程序
//System.Diagnostics.Process.Start(System.Reflection.Assembly.GetExecutingAssembly().Location);
#region 读存app.config字段值
public static string GetConfigValue(string appKey)
{
XmlDocument xDoc = new XmlDocument();
try
{
//缓存路径
xDoc.Load(System.Windows.Forms.Application.ExecutablePath + ".config");
System.Xml.XmlNode xNode;
System.Xml.XmlElement xElem;
xNode = xDoc.SelectSingleNode("//appSettings");
xElem = (System.Xml.XmlElement)xNode.SelectSingleNode("//add[@key='" + appKey + "']");
if (xElem != null)
return xElem.GetAttribute("value");
else
return "";
}
catch
{
return "";
}
}

public static void SetConfigValue(string AppKey, string AppValue)
{
XmlDocument xDoc = new XmlDocument();
xDoc.Load(System.Windows.Forms.Application.ExecutablePath + ".config");

XmlNode xNode;
XmlElement xElem1;
XmlElement xElem2;
xNode = xDoc.SelectSingleNode("//appSettings");

xElem1 = (XmlElement)xNode.SelectSingleNode("//add[@key='" + AppKey + "']");
if (xElem1 != null) xElem1.SetAttribute("value", AppValue);
else
{
xElem2 = xDoc.CreateElement("add");
xElem2.SetAttribute("key", AppKey);
xElem2.SetAttribute("value", AppValue);
xNode.AppendChild(xElem2);
}
xDoc.Save(System.Windows.Forms.Application.ExecutablePath + ".config");
}
#endregion
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: