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

    1. //重启主程序
    2. //System.Diagnostics.Process.Start(System.Reflection.Assembly.GetExecutingAssembly().Location);
    3. #region 读存app.config字段值
    4. public static string GetConfigValue(string appKey)
    5. {
    6. XmlDocument xDoc = new XmlDocument();
    7. try
    8. {
    9. //缓存路径
    10. xDoc.Load(System.Windows.Forms.Application.ExecutablePath + ".config");
    11. System.Xml.XmlNode xNode;
    12. System.Xml.XmlElement xElem;
    13. xNode = xDoc.SelectSingleNode("//appSettings");
    14. xElem = (System.Xml.XmlElement)xNode.SelectSingleNode("//add[@key='" + appKey + "']");
    15. if (xElem != null)
    16. return xElem.GetAttribute("value");
    17. else
    18. return "";
    19. }
    20. catch
    21. {
    22. return "";
    23. }
    24. }
    25. public static void SetConfigValue(string AppKey, string AppValue)
    26. {
    27. XmlDocument xDoc = new XmlDocument();
    28. xDoc.Load(System.Windows.Forms.Application.ExecutablePath + ".config");
    29. XmlNode xNode;
    30. XmlElement xElem1;
    31. XmlElement xElem2;
    32. xNode = xDoc.SelectSingleNode("//appSettings");
    33. xElem1 = (XmlElement)xNode.SelectSingleNode("//add[@key='" + AppKey + "']");
    34. if (xElem1 != null) xElem1.SetAttribute("value", AppValue);
    35. else
    36. {
    37. xElem2 = xDoc.CreateElement("add");
    38. xElem2.SetAttribute("key", AppKey);
    39. xElem2.SetAttribute("value", AppValue);
    40. xNode.AppendChild(xElem2);
    41. }
    42. xDoc.Save(System.Windows.Forms.Application.ExecutablePath + ".config");
    43. }
    44. #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
上一篇:网络-->监控-->单位换算


下一篇:20-C语言结束