使用FileSystemWatcher监视文件变化
本文转载:http://www.cnblogs.com/zanxiaofeng/archive/2011/01/08/1930583.html
FileSystemWatcher基础
属性:
Path——这个属性告诉FileSystemWatcher它需要监控哪条路径。例如,如果我们将这个属性设为“C:\test”,对象就监控test目录下所有文件发生的所有改变(包括删除,修改,创建,重命名)。
IncludeSubDirectories——这个属性说明FileSystemWatcher对象是否应该监控子目录中(所有文件)发生的改变。
Filter——这个属性允许你过滤掉某些类型的文件发生的变化。例如,如果我们只希望在TXT文件被修改/新建/删除时提交通知,可以将这个属性设为“*txt”。在处理高流量或大型目录时,使用这个属性非常方便。
NotifyFilter——获取或设置要监视的更改类型。可以进一步的过滤要监控的更改类型,如watcher.NotifyFilter = NotifyFilters.LastAccess | NotifyFilters.LastWrite
| NotifyFilters.FileName | NotifyFilters.DirectoryName;
事件:
Changed——当被监控的目录中有一个文件被修改时,就提交这个事件。值得注意的是,这个事件可能会被提交多次,即使文件的内容仅仅发生一项改变。这是由于在保存文件时,文件的其它属性也发生了改变。
Created——当被监控的目录新建一个文件时,就提交这个事件。如果你计划用这个事件移动新建的事件,你必须在事件处理器中写入一些错误处理代码,它能处理当前文件被其它进程使用的情况。之所以要这样做,是因为Created事件可能在建立文件的进程释放文件之前就被提交。如果你没有准备正确处理这种情况的代码,就可能出现异常。
Deleted——当被监控的目录中有一个文件被删除,就提交这个事件。
Renamed——当被监控的目录中有一个文件被重命名,就提交这个事件。
注:如果你没有将EnableRaisingEvents设为真,系统不会提交任何一个事件。如果有时FileSystemWatcher对象似乎无法工作,请首先检查EnableRaisingEvents,确保它被设为真。
问题:
程序里需要监视某个目录下的文件变化情况: 一旦目录中出现新文件或者旧的文件被覆盖,程序需要读取文件内容并进行处理;但在实际处理中发现当一个文件产生变化时,Change事件被反复触发了好几次。这样可能的结果是造成同一文件的重复处理。
解决方法:
针对上面的问题,于是写了一个可以延迟FileSystemWatcher发出的事件的Class DelayFileSystemWatcher。

using System;
using System.Collections.Generic;
using System.IO;
using System.Threading; namespace Utility
{
public class DelayFileSystemWatcher
{
private readonly Timer m_Timer;
private readonly Int32 m_TimerInterval;
private readonly FileSystemWatcher m_FileSystemWatcher;
private readonly FileSystemEventHandler m_FileSystemEventHandler;
private readonly Dictionary<String, FileSystemEventArgs> m_ChangedFiles = new Dictionary<string, FileSystemEventArgs>(); public DelayFileSystemWatcher(string path, string filter, FileSystemEventHandler watchHandler, int timerInterval)
{
m_Timer = new Timer(OnTimer, null, Timeout.Infinite, Timeout.Infinite);
m_FileSystemWatcher = new FileSystemWatcher(path, filter);
m_FileSystemWatcher.NotifyFilter = NotifyFilters.FileName | NotifyFilters.LastWrite | NotifyFilters.CreationTime;
m_FileSystemWatcher.Created += fileSystemWatcher_Changed;
m_FileSystemWatcher.Changed += fileSystemWatcher_Changed;
m_FileSystemWatcher.Deleted += fileSystemWatcher_Changed;
m_FileSystemWatcher.Renamed += fileSystemWatcher_Changed;
m_FileSystemWatcher.EnableRaisingEvents = true;
m_FileSystemEventHandler = watchHandler;
m_TimerInterval = timerInterval;
} public void fileSystemWatcher_Changed(object sender, FileSystemEventArgs e)
{
lock (m_ChangedFiles)
{
if (!m_ChangedFiles.ContainsKey(e.Name))
{
m_ChangedFiles.Add(e.Name, e);
}
}
m_Timer.Change(m_TimerInterval, Timeout.Infinite);
} private void OnTimer(object state)
{
Dictionary<String, FileSystemEventArgs> tempChangedFiles = new Dictionary<String, FileSystemEventArgs>(); lock (m_ChangedFiles)
{
foreach (KeyValuePair<string, FileSystemEventArgs> changedFile in m_ChangedFiles)
{
tempChangedFiles.Add(changedFile.Key, changedFile.Value);
}
m_ChangedFiles.Clear();
} foreach (KeyValuePair<string, FileSystemEventArgs> changedFile in tempChangedFiles)
{
m_FileSystemEventHandler(this, changedFile.Value);
}
}
}
}

使用方式如下:

s_DelayFileSystemWatcher = new DelayFileSystemWatcher(@"C:\Temp", "*.xml", fileSystemWatcher_Changed, 1500);
private static void fileSystemWatcher_Changed(object sender, FileSystemEventArgs e)
{
switch (e.ChangeType)
{
case WatcherChangeTypes.Created:
//TODO
break;
case WatcherChangeTypes.Deleted:
//TODO
break;
case WatcherChangeTypes.Changed:
//TODO
break;
default:
break;
}
}

使用FileSystemWatcher监视文件变化的更多相关文章
- Webpack 热部署检测不到文件变化问题
Webpack 热部署检测不到文件变化问题 今天在用Webpack开发的时候,突然发现文件变动后热部署功能不工作了,感觉好像是webpack检测不到文件的修改了.折腾了半天,开始一直以为是自己的代码有 ...
- node监视文件或者文件夹的变化
1.node提供一个watch这个API,可以监视文件或文件夹的变化,当发生改变时,做出某些操作.(当做代码本地构建时,经常使用) const fs = require('fs'); fs.watch ...
- C# FileSystemWatcher 监视磁盘文件变更
简化需求:有一个简化了的需求是这样的:有一个拍照程序在运行,一旦抓拍之后则将图片文件存储至某目录,然后图片要上传至远程服务器并update数据库. 原需求:原先的需求是这样的:有一台PDA扫码枪,一个 ...
- C# FileSystemWatcher 监视磁盘文件
C# FileSystemWatcher 监视磁盘文件变更 简化需求:有一个简化了的需求是这样的:有一个拍照程序在运行,一旦抓拍之后则将图片文件存储至某目录,然后图片要上传至远程服务器并update数 ...
- FileSystemWatcher 监视指定目录中的变更
.Net框架类库中的FileSystemWatcher如它的名称一样是一个用于监视文件系统变化的一个控件.使用 FileSystemWatcher 监视指定目录中的更改.可监视指定目录中的文件或子目录 ...
- watchdog监控文件变化使用总结——转载
原文链接地址:https://blog.csdn.net/xufive/article/details/93847372 概述 首先声明,本文讨论的 watchdog,不是单片机里的 watchdog ...
- C#监控指定目录的文件变化的代码
如下的资料是关于C#监控指定目录的文件变化的代码. FileSystemWatcher watcher = new FileSystemWatcher();watcher.Path = @" ...
- 使用FileSystemWatcher监视指定目录
使用 FileSystemWatcher 监视指定目录中的更改.可监视指定目录中的文件或子目录的更改. 以下是一个简单的实例,用来监控指定目录下文件的新增.删除.重命名等情况(文件内容更改会触发多次, ...
- Linux 监视文件、文件夹改动
/******************************************************************** * Linux 监视文件.文件夹改动 * 说明: * 主要是 ...
随机推荐
- CSS3中的background-size(对响应性图片等比例缩放)
background-size的基本属性 background-size: 可以设定背景图像的尺寸,该属性是css3中的,在移动端使用的地方很多,比如最常见的地方在做响应性布局的时候,比如之前做的项目 ...
- 博客系统-3.0CodeIgniter系统SAE版本的配置 application/config/
autoload.php(系统启动时自动加载的文件:包,类库,驱动,方法助手,配置) $autoload['libraries'] = array('database', 'access', 'pag ...
- Android App的生命周期是什么
怎么说呢 看Android一般指的是 Activity的生命周期, 关于app的生命周期, 有明白的大神请告诉我 上面这张图是 网上搜到的一张关于app生命周期的图, 在我看来, 其实就是一个Acti ...
- Objective-C 入门(给新人的)
http://www.hengxinsoft.com/2010/12/objective-c-%E5%85%A5%E9%97%A8%EF%BC%88%E7%BB%99%E6%96%B0%E4%BA%B ...
- hdu 1576 A/B 拓展欧几里得算法
A/B Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submiss ...
- Android UI基础教程 目录
从csdn下载了这本英文版的书之后,又去京东搞了一个中文目录下来.对照着看. 话说,这本书绝对超值.有money的童鞋看完英文版记得去买中文版的~~ Android UI基础教程完整英文版 pdf+源 ...
- HTML Imports
为什么需要导入? 先想想你在 web 上是如何加载不同类型的资源.对于 JS,我们有 <script src>.<link rel="stylesheet"> ...
- HTTP-304 NOT Modified
http://www.douban.com/note/161120791/ http://blog.sina.com.cn/s/blog_4c98b9600100jd4z.html http://ww ...
- c语言exit和return区别,在fork和vfork中使用
转自c语言exit和return区别,在fork和vfork中使用 exit函数在头文件stdlib.h中. 简述: exit(0):正常运行程序并退出程序: exit(1):非正常运行导致退出程序: ...
- Long Long Message
poj2774:http://poj.org/problem?id=2774 题意:求两个串的最长公共子串. 题解:求出后缀数组,然后求height数组,找出最大的值,并且这两个子串在不同的原串中即可 ...