• WinForm操作config文件


    往配置文件中保存配置信息

    View Code
     1 public void SetConfigValue(string AppKey, string AppValue)
     2         {
     3             XmlDocument xDoc = new XmlDocument();
     4             xDoc.Load(System.Windows.Forms.Application.ExecutablePath + ".config");
     5             XmlNode xNode;
     6             XmlElement xElem1;
     7             XmlElement xElem2;
     8             xNode = xDoc.SelectSingleNode("//appSettings");
     9             xElem1 = (XmlElement)xNode.SelectSingleNode("//add[@key='" + AppKey + "']");
    10             if (xElem1 != null)
    11                 xElem1.SetAttribute("value", AppValue);
    12             else//如果没有该节点,则添加
    13             {
    14                 xElem2 = xDoc.CreateElement("add");
    15                 xElem2.SetAttribute("key", AppKey);
    16                 xElem2.SetAttribute("value", AppValue);
    17                 xNode.AppendChild(xElem2);
    18             }
    19             xDoc.Save(System.Windows.Forms.Application.ExecutablePath + ".config");
    20         }

    根据节点名称读取配置信息

    View Code
     1 public string GetConfigValue(string AppKey)
     2         {
     3             XmlDocument xDoc = new XmlDocument();
     4             try
     5             {
     6                 xDoc.Load(System.Windows.Forms.Application.ExecutablePath + ".config");
     7                 XmlNode xNode = xDoc.SelectSingleNode("//appSettings");
     8                 XmlElement xElem = (XmlElement)xNode.SelectSingleNode("//add[@key='" + AppKey + "']");
     9                 if (xElem != null)
    10                     return xElem.GetAttribute("value");
    11                 else
    12                     return "";
    13             }
    14             catch (Exception ex)
    15             {
    16                 return ex.ToString();
    17             }
    18         }
  • 相关阅读:
    linuxshell中"2>&1"含义
    Java中正数与负数操作>>、>>>的区别
    jsp el表达式
    struct2常用标签
    shell正则表达式
    IPV6学起来很费力?你看看,也就这样简单吧!
    STP、RSTP、MSTP合集思维导图
    HCIE之路--ISIS思维导图
    佛祖保佑,永不宕机! 永无BUG!
    震惊!ARP安全竟然还可以这样配置?
  • 原文地址:https://www.cnblogs.com/Casker/p/2987182.html
Copyright © 2020-2023  润新知