zoukankan      html  css  js  c++  java
  • 配置文件监听器

    主要是根据FileSystemWatcher类来监听配置文件

    目标是每次修改了web.config配置文件不用回收应用程序或者重启网站

    贴出实现核心代码

      1     /// <summary>
      2     /// 配置文件监听器
      3     /// </summary>
      4     public sealed class ConfigMonitor
      5     {
      6         /// <summary>
      7         /// 配置文件路径
      8         /// </summary>
      9         private static string path = AppDomain.CurrentDomain.SetupInformation.ApplicationBase;
     10 
     11         /// <summary>
     12         /// 配置文件路径
     13         /// </summary>
     14         public static string Path
     15         {
     16             get
     17             {
     18                 return path;
     19             }
     20 
     21             set
     22             {
     23                 path = value;
     24             }
     25         }
     26 
     27         /// <summary>
     28         /// 配置文件名称
     29         /// </summary>
     30         private static string filter = "\Web.config";
     31 
     32         /// <summary>
     33         /// 配置文件名称
     34         /// </summary>
     35         public static string Filter
     36         {
     37             get
     38             {
     39                 return filter;
     40             }
     41 
     42             set
     43             {
     44                 filter = value;
     45             }
     46         }
     47 
     48         /// <summary>
     49         /// 配置文件更改委托定义
     50         /// </summary>
     51         public delegate void EventHandlerAfterConfigModify();
     52 
     53         /// <summary>
     54         /// 配置文件更改事件
     55         /// </summary>
     56         public static event EventHandlerAfterConfigModify ConfigModifyInfoEvent;
     57 
     58         /// <summary>
     59         /// 配置对象
     60         /// </summary>
     61         public static Configuration Config { get; private set; }
     62 
     63         /// <summary>
     64         /// Initializes static members of the <see cref="ConfigMonitor"/> class.
     65         /// 构造函数
     66         /// </summary>
     67         static ConfigMonitor()
     68         {
     69             MonitorConfigFile();
     70             InitConnectionConfig();
     71         }
     72 
     73         /// <summary>
     74         /// 创建配置文件发动监听器
     75         /// </summary>
     76         private static void MonitorConfigFile()
     77         {
     78             FileSystemWatcher fileWatcher = new FileSystemWatcher
     79                                                 {
     80                                                     Path = Path,
     81                                                     Filter = Filter,
     82                                                     NotifyFilter = NotifyFilters.CreationTime | NotifyFilters.LastWrite | NotifyFilters.FileName
     83                                                 };
     84 
     85             fileWatcher.Changed += OnChanged;
     86             fileWatcher.Created += OnChanged;
     87             fileWatcher.Deleted += OnChanged;
     88             fileWatcher.Renamed += OnChanged;
     89             fileWatcher.EnableRaisingEvents = true;
     90         }
     91 
     92         /// <summary>
     93         /// 更改处理事件
     94         /// </summary>
     95         /// <param name="source">事件源</param>
     96         /// <param name="e">事件参数</param>
     97         private static void OnChanged(object source, FileSystemEventArgs e)
     98         {
     99             InitConnectionConfig();
    100             RaiseEvent();
    101         }
    102 
    103         /// <summary>
    104         /// 向订阅者发布信息
    105         /// </summary>
    106         private static void RaiseEvent()
    107         {
    108             if (ConfigModifyInfoEvent != null)
    109             {
    110                 ConfigModifyInfoEvent();
    111             }
    112         }
    113 
    114         /// <summary>
    115         /// 初始化所有连接配置
    116         /// </summary>
    117         private static void InitConnectionConfig()
    118         {
    119             string filePath = Path + Filter;
    120             var map = new ExeConfigurationFileMap { ExeConfigFilename = filePath };
    121             Config = ConfigurationManager.OpenMappedExeConfiguration(map, ConfigurationUserLevel.None);
    122         }
    123     }
    View Code
  • 相关阅读:
    Hadoop开发周期(一):基础环境安装
    Laravel5中使用阿里大于(鱼)发送短信验证码
    整理关于web项目如何防止CSRF和XSS攻击的方法
    TP5.0 excel 导入导出
    eoLinker_业内领先的api管理平台
    ImageIcon读取文件 规格严格
    JPDA Trace好不容易找到的 规格严格
    FileLocker 规格严格
    debugging information corrupt; recompile module 规格严格
    Windows架设FTP 规格严格
  • 原文地址:https://www.cnblogs.com/liuxiaoji/p/4290492.html
Copyright © 2011-2022 走看看