• 读取,设置指定目录下的配置文件的节点key value。config或XML


    public static string GetValue(string key)//读取指定节点
            {
                XmlDocument doc = new XmlDocument();
                string nodeVal = string.Empty;
                try
                {
                    doc.Load(HttpContext.Current.Server.MapPath("/Config/AppSetings.config"));//路径
                    XmlNode node;
                    XmlElement element;
                    node = doc.SelectSingleNode("//" + "appSettings");//父节点
                    element = (XmlElement)node.SelectSingleNode("//add[@key='" + key + "']");//add 节点的 key名称
                    if (element != null)
                    {
                        nodeVal = element.GetAttribute("value");
                    }
                    return nodeVal;
                }
                catch (Exception)
                {
                    return "";
                    throw;
                }
            }
    /// <summary>
            /// 根据Key修改Value
            /// </summary>
            /// <param name="key">要修改的Key</param>
            /// <param name="value">要修改为的值</param>
            public static void SetValue(string key, string value)
            {
                FileInfo fi = new FileInfo(HttpContext.Current.Server.MapPath("/Config/AppSetings.config"));
                //判断文件属性是否只读?是则修改为一般属性
                if (fi.IsReadOnly==true)
                {
                    fi.IsReadOnly = false;
                }
                System.Xml.XmlDocument xDoc = new System.Xml.XmlDocument();
                xDoc.Load(HttpContext.Current.Server.MapPath("/Config/AppSetings.config"));
                System.Xml.XmlNode xNode;
                System.Xml.XmlElement xElem1;
                System.Xml.XmlElement xElem2;
                xNode = xDoc.SelectSingleNode("//appSettings");
    
                xElem1 = (System.Xml.XmlElement)xNode.SelectSingleNode("//add[@key='" + key + "']");
                if (xElem1 != null) xElem1.SetAttribute("value", value);
                else
                {
                    xElem2 = xDoc.CreateElement("add");
                    xElem2.SetAttribute("key", key);
                    xElem2.SetAttribute("value", value);
                    xNode.AppendChild(xElem2);
                }
                xDoc.Save(HttpContext.Current.Server.MapPath("/Config/AppSetings.config"));
            }
    

    第二种方法修改

    /// <summary>
    
            /// 修改web.config中appSettings键的值
            /// </summary>
            /// <param name="configPath">web.config路径</param>
            /// <param name="keyName">键的名称</param>
            /// <param name="keyValue">键的值</param>
            public void UpdAppSettings(string configPath,string keyName,string keyValue)
            {
                XmlDocument doc = new XmlDocument();
                try
                {
                    doc.Load(configPath);
                    XmlNode node;
                    XmlElement element;
                    node = doc.SelectSingleNode("//appSettings");
                    element = (XmlElement)node.SelectSingleNode("//add[@key='" + keyName + "']");
                    if (element != null)
                    {
                        element.SetAttribute("value", keyValue);
                        doc.Save(configPath);
                    }
                }
                catch (Exception)
                {
                    throw;
                }
            }
    

    Config.config

    <?xml version="1.0" encoding="utf-8"?>
    <configuration>
      <appSettings>
        <add key="IntervalTime" value="12"/><!--扫码间隔时间-->
        <add key="FCKeditor" value="Files"/>
      </appSettings>
      <system.web>
    
      </system.web>
    </configuration>
    
  • 相关阅读:
    新华字典有多少字
    lisp install
    OCaml Language Sucks
    Erlang, Haskell, OCaml: screw one, marry one, kill one. Which and why?
    Linux获取网页源码的几种方法
    什么是zhcon
    What is plowshare?
    neo4j简单学习
    neo4j 云端部署
    Clojure语言 vs Scala语言
  • 原文地址:https://www.cnblogs.com/wybshyy/p/13783706.html
Copyright © 2020-2023  润新知