zoukankan      html  css  js  c++  java
  • 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         }
  • 相关阅读:
    js获取数组,对象的真实长度
    http和https区别
    react调用setstate后发生了什么
    for in for of foreach及map的区别
    事件委托(事件代理)
    CSS隐藏元素的几种方法
    react一些扩展
    [软件构造]异常的捕获与自定义
    [软件构造]可能是笔记总结吧
    计算机系统大作业
  • 原文地址:https://www.cnblogs.com/Casker/p/2987182.html
Copyright © 2011-2022 走看看