zoukankan      html  css  js  c++  java
  • 读取,设置指定目录下的配置文件的节点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>
    
  • 相关阅读:
    嵌入式
    IT 管理
    linux 网络编程 排序
    linux frameBuffer
    虚拟现实
    vc 串口
    OpenGLES 图像
    runloop
    归档
    商标查询
  • 原文地址:https://www.cnblogs.com/wybshyy/p/13783706.html
Copyright © 2011-2022 走看看