使用 FileSystemWatcher 侦听文件

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;                            
    }
                                
}
                                    
 
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值