使用 FileSystemWatcher 侦听文件

来源:互联网 发布:c语言数据结构有哪些 编辑:程序博客网 时间:2024/04/29 04:48
private ConfigureAndWatchHandler(ILoggerRepository repository, FileInfo configFile)                                    
{                                    
    
// Create a new FileSystemWatcher and set its properties.                                
    FileSystemWatcher watcher = new FileSystemWatcher();                                
                                    
    watcher.Path 
= m_configFile.DirectoryName;    //设定侦听文件的目录                                
    watcher.Filter = m_configFile.Name;                   //设定指定的侦听文件,不设置,则侦听整个目录                                
                                    
    
// Set the notification filters 设定侦听文件的哪些属性                                
    watcher.NotifyFilter = NotifyFilters.CreationTime | NotifyFilters.LastWrite | NotifyFilters.FileName;                                
                                    
    
// Add event handlers. OnChanged will do for all event handlers that fire a FileSystemEventArgs                                
    watcher.Changed += new FileSystemEventHandler(ConfigureAndWatchHandler_OnChanged);                                
    watcher.Created 
+= new FileSystemEventHandler(ConfigureAndWatchHandler_OnChanged);                                
    watcher.Deleted 
+= new FileSystemEventHandler(ConfigureAndWatchHandler_OnChanged);                                
    watcher.Renamed 
+= new RenamedEventHandler(ConfigureAndWatchHandler_OnRenamed);                                
                                    
    
// Begin watching. 设定是否启用侦听                                
    watcher.EnableRaisingEvents = true;                                
                                    
    
//由于修改了侦听文件,会激发2次watcher的Changed事件,所以用Timer来延迟主处理                                
    
// Create the timer that will be used to deliver events. Set as disabled                                
    _timer = new Timer(new TimerCallback(OnWatchedFileChange), null, Timeout.Infinite, Timeout.Infinite);                                
}
                                    
                                    
private Timer _timer;                                    
private const int TimeoutMillis = 500;                                    
private void Watcher_Changed(object sender, FileSystemEventArgs e)                                    
{                                    
    
// Deliver the event in TimeoutMillis time                                
    
// timer will fire only once                                
    _timer.Change(TimeoutMillis, Timeout.Infinite);                                
}
                                    
private void OnWatchedFileChange(object state)                                    
{                                    
    
try                                
    
{                                
        
this._isReload = true;                            
        
this._mappers = this.CetMappers();                            
    }
                                
    
catch {}                                
    
finally                                
    
{                                
        
this._isReload = false;                            
    }
                                
}
                                    
 
原创粉丝点击