获取、修改指定路径web.config appSettings键的值

  /// <summary>
        /// 读取add节点中 key的value
        /// </summary>
        /// <param name="readPath">路径</param>
        /// <param name="parentNode">父节点</param>
        /// <param name="keyName">add 节点的 key名称</param>
        /// <returns></returns>
        public static string ReadAddNodeByKey(string readPath, string parentNode, string keyName)
        {
            XmlDocument doc = new XmlDocument();
            string nodeVal = string.Empty;
            try
            {
                doc.Load(readPath);
                XmlNode node;
                XmlElement element;
                node = doc.SelectSingleNode("//" + parentNode);
                element = (XmlElement)node.SelectSingleNode("//add[@key='" + keyName + "']");
                if (element != null)
                {
                    nodeVal = element.GetAttribute("value");
                }
                return nodeVal;
            }
            catch (Exception)
            {
                throw;
            }
        }
       


       /// <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;
            }

        }


      #region 修改配置文件信息
        /// <summary>
        /// 修改配置文件信息
        /// </summary>
        /// <param name="filePath">文件路径</param>
        /// <param name="keyName">配置名称</param>
        /// <param name="value">配置值</param>
        public static void EditConfigVal(string filePath, string keyName, string value)
        {
            XmlDocument doc = new XmlDocument();
            try
            {
                doc.Load(filePath);
                XmlNode node;
                XmlElement element;
                node = doc.SelectSingleNode("//appSettings");
                element = (XmlElement)node.SelectSingleNode("//add[@key='" + keyName + "']");
                element.SetAttribute("value", value);
                doc.Save(filePath);
            }
            catch
            {
                throw;
            }
        }
        #endregion


        #region 获取配置文件信息
        /// <summary>
        /// 获取配置文件信息
        /// </summary>
        /// <param name="filePath">文件路径</param>
        /// <param name="keyName">key名称</param>
        /// <returns>返回key的value</returns>
        public static string GetConfigVal(string filePath,string keyName)
        {
            string val = "";
            XmlDocument doc = new XmlDocument();
            try
            {
                FileInfo fi = new FileInfo(filePath);
                XmlNode node;
                XmlElement element;
                if (fi.Exists)
                {
                    doc.Load(fi.FullName);
                    node = doc.SelectSingleNode("//appSettings");
                    element = (XmlElement)node.SelectSingleNode("//add[@key='" + keyName + "']");
                    if (element != null)
                    {
                        val=element.Attributes.Item(1).InnerText;
                    }
                }
            }
            catch (Exception)
            {
                throw;
            }
            return val;
        }
        #endregion


  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值