zoukankan      html  css  js  c++  java
  • 动态操作.Config文件

    动态操作.Config文件

    引自:http://www.cnblogs.com/RuiLei/archive/2008/06/11/1217576.html

     1    /// <summary>
     2        /// Dynamic Write App.config
     3        /// </summary>
     4        /// <param name="AppKey"></param>
     5        /// <param name="AppValue"></param>

     6        public static void SetValue(string AppKey, string AppValue)
     7        {
     8            XmlDocument xDoc = new XmlDocument();
     9            //获取可执行文件的路径和名称
    10            xDoc.Load(System.Windows.Forms.Application.ExecutablePath + ".config");
    11
    12            XmlNode xNode;
    13            XmlElement xElem1;
    14            XmlElement xElem2;
    15            xNode = xDoc.SelectSingleNode("//appSettings");
    16
    17            xElem1 = (XmlElement)xNode.SelectSingleNode("//add[@key='" + AppKey + "']");
    18            if (xElem1 != null) xElem1.SetAttribute("value", AppValue);
    19            else
    20            {
    21                xElem2 = xDoc.CreateElement("add");
    22                xElem2.SetAttribute("key", AppKey);
    23                xElem2.SetAttribute("value", AppValue);
    24                xNode.AppendChild(xElem2);
    25            }

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

    28
    29        /// <summary>
    30        /// Reader App.config
    31        /// </summary>
    32        /// <param name="appKey"></param>
    33        /// <returns></returns>

    34        public static string GetConfigValue(string appKey)
    35        {
    36            XmlDocument xDoc = new XmlDocument();
    37            try
    38            {
    39                xDoc.Load(System.Windows.Forms.Application.ExecutablePath + ".config");
    40
    41                XmlNode xNode;
    42                XmlElement xElem;
    43                xNode = xDoc.SelectSingleNode("//appSettings");
    44                xElem = (XmlElement)xNode.SelectSingleNode("//add[@key='" + appKey + "']");
    45                if (xElem != null)
    46                    return xElem.GetAttribute("value");
    47                else
    48                    return String.Empty;
    49            }

    50            catch (Exception)
    51            {
    52                return "";
    53            }

    54        }

  • 相关阅读:
    洛谷P5661 公交换乘(二分)
    洛谷P4047 [JSOI2010]部落划分(最小生成树)
    洛谷P2872 [USACO07DEC]Building Roads S(最小生成树)
    卸载重装VirtualBox回滚报错
    POJ1151 Atlantis(扫描线+线段树+离散化)
    QT入门-信号槽拓展
    Vue模板语法与常用指令总结
    Vue 生命周期
    querySelector和getElementById方法的区别
    ES6 Class(类)的继承与常用方法
  • 原文地址:https://www.cnblogs.com/sgivee/p/1744852.html
Copyright © 2011-2022 走看看