学无止境,精益求精

十年河东,十年河西,莫欺少年穷

学历代表你的过去,能力代表你的现在,学习代表你的将来

配置文件有两种设置方式,第一种是直接在网站根目录下的web.config中设置;第二种方式是自定义配置文件,在web.config中指定其他配置文件的路径。

第一种:除了在常用的appSettings节点下进行<add/>添加,还可以在<configSections>节点下设置<section/>节点,

第二种:完全和web.config没有关联

关于第一种,大家可参考博客:https://www.cnblogs.com/len0031/p/7571594.html

本篇主要讲解第二种

我们都知道,在项目发布后,如果在生产环境修改配置文件,会重启IIS,这也就意味着会刷新所有服务,会影响用户的使用,导用户的操作失败。

因此,在大项目中,我们的许多配置除了使用配置文件外,还可以使用自定义的配置文件,这种配置文件修改时,不会影响用户的使用,这就是使用这类配置文件的好处。

其实,配置文件就是一个XML文件,所谓操作自定义配置文件其核心就是操作XML文件

我们在项目根目录新建一个SelectItem.config,如下:

<?xml version="1.0" encoding="utf-8"?>
<!--自定义配置文件,用于配置项-->
<config>
  <!-- 同种类型的验证码,每天最多的接收次数-->
  <dataConfig value="AuthCodeCount">10</dataConfig>
  <!-- 发送验证码后,有效时长(分钟)-->
  <dataConfig value="AuthCodeTime">2</dataConfig>
  <!-- 发送验证码后,JS控制效果时长(120秒)-->
  <dataConfig value="AuthCodeTimeLong">120</dataConfig>
</config>

那么如何读取这个文件呢?

在项目的公共方法层新建如下类:SelectItemConfig,用于读取XML文件,如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;

namespace Common
{
    public class SelectItemConfig
    {
        private XmlDocument _doc;
        private XmlNode _rootNode;

        public SelectItemConfig(String mapPath, string name)
        {
            _doc = new XmlDocument();
            _doc.Load(mapPath + name);
            _rootNode = _doc.DocumentElement;
        }

        public void setConfig(String key, String value)
        {
            if (value == null || (value != null && value.Length == 0)) { return; }

            if (_rootNode.SelectSingleNode(key) == null || (_rootNode.SelectSingleNode(key) != null && _rootNode.SelectSingleNode(key).ChildNodes.Count == 0))
            {
                XmlElement newNode = _doc.CreateElement(key);
                newNode.InnerText = value;
                _rootNode.AppendChild(newNode);
            }
            else
            {
                _rootNode.SelectSingleNode(key).ChildNodes[0].InnerText = value;
            }
        }

        /// <summary>
        /// 获取单个节点的值
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public String getConfig(string key)
        {
            if (_rootNode.SelectSingleNode(key) != null)
                if (_rootNode.SelectSingleNode(key).ChildNodes.Count > 0)
                    return _rootNode.SelectSingleNode(key).ChildNodes[0].Value;
                else
                    return "";
            else
                return null;
        }

        /// <summary>
        /// 获取key值对应的多个节点的值的集合
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public Dictionary<string, string> getItemListByKey(string key)
        {
            Dictionary<string, string> dic = new Dictionary<string, string>() { };
            if (_rootNode.SelectSingleNode(key) != null)
            {
                XmlNodeList xmlNode = _rootNode.SelectNodes(key);
                int nodeCount = _rootNode.SelectNodes(key).Count;
                if (nodeCount > 0)
                {
                    for (int i = 0; i < nodeCount; i++)
                    {
                        var item = _rootNode.SelectNodes(key).Item(i);
                        dic.Add(item.Attributes["value"].Value, item.InnerText);//Select 下拉框 Value属性
                    }
                }
            }
            return dic;
        }

        public Boolean SaveConfig(String mapPath)
        {
            try
            {
                _doc.Save(mapPath + @"FlexPaper.config");
                return true;
            }
            catch
            {
                return false;
            }
        }
    }
}
View Code

相关文章: