使用 FileSystemWatcher 侦听文件
2006-09-10 11:45
453 查看
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;
}
}
相关文章推荐
- 谈谈C#文件监控对象FileSystemWatcher使用感受
- 在C#使用文件监控对象FileSystemWatcher的几种方案
- FileSystemWatcher的使用--文件更改监视器
- 使用FileSystemWatcher监视文件更改
- 结合 FileSystemWatcher更好的使用配置文件
- 使用filesystemwatcher时怎么处理大文件
- 谈谈C#文件监控对象FileSystemWatcher使用感受
- C#~使用FileSystemWatcher来监视文件系统的变化
- c#使用filesystemwatcher实时监控文件目录的添加和删除
- 在C#使用文件监控对象FileSystemWatcher 实现数据同步
- 谈谈C#文件监控对象FileSystemWatcher使用感受
- 结合 FileSystemWatcher更好的使用配置文件
- 谈谈C#文件监控对象FileSystemWatcher使用感受
- c#使用filesystemwatcher监视文件系统的变化
- 使用FileSystemWatcher响应文件系统事件
- 在ASP.NET中使用FileSystemWatcher来监控文件目录
- C# 使用FileSystemWatcher来监视文件系统的变化
- 谈谈C#文件监控对象FileSystemWatcher使用感受
- 使用文件监控对象FileSystemWatcher实现数据同步
- C#使用文件监控对象FileSystemWatcher 实现数据同步