使用 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;
}
}
...{
// 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;
}
}
- 使用 FileSystemWatcher 侦听文件
- 使用FileSystemWatcher监控文件变化
- 文件监视器( FileSystemWatcher) 类的使用
- FileSystemWatcher的使用--文件更改监视器
- 谈谈C#文件监控对象FileSystemWatcher使用感受
- 谈谈C#文件监控对象FileSystemWatcher使用感受
- 使用.NET FileSystemWatcher对象监控磁盘文件目录的改变
- 在C#使用文件监控对象FileSystemWatcher 实现数据同步
- 在C#使用文件监控对象FileSystemWatcher 实现数据同步
- 谈谈C#文件监控对象FileSystemWatcher使用感受
- 谈谈C#文件监控对象FileSystemWatcher使用感受
- FileSystemWatcher文件文件夹监听
- FileSystemWatcher文件监控
- FileSystemWatcher 的使用
- android 侦听文件变化
- 用FileSystemWatcher监视文件变化
- FileSystemWatcher判断文件复制完成
- C# FileSystemWatcher监控指定文件
- 求最长递增子序列问题
- 更改BLOG地址
- JAVA字符集
- 计划经济下的货币
- 弱水三千,只取一瓢饮 的意思
- 使用 FileSystemWatcher 侦听文件
- 数独游戏的生成算法
- 在vs2005.net中,用C#如何将xml文件转换成二进制数据存入数据库字段,并从数据库提取该字段转换成xml文件?
- 超键,候选键,主键
- 将动态页面转化成二进制字节流
- 根据表格内容,自动调整VB.NET中Datagrid控件单元格宽度
- 构建基本的.NET Remoting应用程序
- 极品飞车9最高通缉
- XMLBEANS实践——自动化XML和Java之间的数据交换(一)