配置文件监听器

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

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

贴出实现核心代码

  1     /// 
  2     /// 配置文件监听器
  3     /// 
  4     public sealed class ConfigMonitor
  5     {
  6         /// 
  7         /// 配置文件路径
  8         /// 
  9         private static string path = AppDomain.CurrentDomain.SetupInformation.ApplicationBase;
 10 
 11         /// 
 12         /// 配置文件路径
 13         /// 
 14         public static string Path
 15         {
 16             get
 17             {
 18                 return path;
 19             }
 20 
 21             set
 22             {
 23                 path = value;
 24             }
 25         }
 26 
 27         /// 
 28         /// 配置文件名称
 29         /// 
 30         private static string filter = "\\Web.config";
 31 
 32         /// 
 33         /// 配置文件名称
 34         /// 
 35         public static string Filter
 36         {
 37             get
 38             {
 39                 return filter;
 40             }
 41 
 42             set
 43             {
 44                 filter = value;
 45             }
 46         }
 47 
 48         /// 
 49         /// 配置文件更改委托定义
 50         /// 
 51         public delegate void EventHandlerAfterConfigModify();
 52 
 53         /// 
 54         /// 配置文件更改事件
 55         /// 
 56         public static event EventHandlerAfterConfigModify ConfigModifyInfoEvent;
 57 
 58         /// 
 59         /// 配置对象
 60         /// 
 61         public static Configuration Config { get; private set; }
 62 
 63         /// 
 64         /// Initializes static members of the  class.
 65         /// 构造函数
 66         /// 
 67         static ConfigMonitor()
 68         {
 69             MonitorConfigFile();
 70             InitConnectionConfig();
 71         }
 72 
 73         /// 
 74         /// 创建配置文件发动监听器
 75         /// 
 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         /// 
 93         /// 更改处理事件
 94         /// 
 95         /// 事件源
 96         /// 事件参数
 97         private static void OnChanged(object source, FileSystemEventArgs e)
 98         {
 99             InitConnectionConfig();
100             RaiseEvent();
101         }
102 
103         /// 
104         /// 向订阅者发布信息
105         /// 
106         private static void RaiseEvent()
107         {
108             if (ConfigModifyInfoEvent != null)
109             {
110                 ConfigModifyInfoEvent();
111             }
112         }
113 
114         /// 
115         /// 初始化所有连接配置
116         /// 
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

 

转载于:https://www.cnblogs.com/liuxiaoji/p/4290492.html

你可能感兴趣的:(配置文件监听器)