c#使用filesystemwatcher实时监控文件目录的添加和删除
首先,我们需要对.net提供的FileSystemWatcher类有所了解。我有些懒,找了MSDN对该类的描述。
FileSystemWatcher类侦听文件系统更改通知,并在目录或目录中的文件发生更改时引发事件。
使用FileSystemWatcher监视指定目录中的更改。可监视指定目录中的文件或子目录的更改。可以创建一个组件来监视本地计算机、网络驱动器或远程计算机上的文件。
若要监视所有文件中的更改,请将Filter属性设置为空字符串("")或使用通配符(“*.*”)。若要监视特定的文件,请将Filter属性设置为该文件名。例如,若要监视文件MyDoc.txt中的更改,请将Filter属性设置为“MyDoc.txt”。也可以监视特定类型文件中的更改。例如,若要监视文本文件中的更改,请将Filter属性设置为“*.txt”。
可监视目录或文件中的若干种更改。例如,可监视文件或目录的Attributes、LastWrite日期和时间或Size方面的更改。通过将NotifyFilter属性设置为NotifyFilters值之一来达到此目的。有关可监视的更改类型的更多信息,请参见NotifyFilters。
可监视文件或目录的重命名、删除或创建。例如,若要监视文本文件的重命名,请将Filter属性设置为“*.txt”,并使用为其参数指定的Renamed来调用WaitForChanged方法。
Windows操作系统在FileSystemWatcher创建的缓冲区中通知组件文件发生更改。如果短时间内有很多更改,则缓冲区可能会溢出。这将导致组件失去对目录更改的跟踪,并且它将只提供一般性通知。使用InternalBufferSize属性来增加缓冲区大小的开销较大,因为它来自无法换出到磁盘的非页面内存,所以应确保缓冲区大小适中(尽量小,但也要有足够大小以便不会丢失任何文件更改事件)。若要避免缓冲区溢出,请使用NotifyFilter和IncludeSubdirectories属性,以便可以筛选掉不想要的更改通知。
使用FileSystemWatcher类时,请注意以下事项。
1)对包括隐藏文件(夹)在内的所有文件(夹)进行监控。
2)您可以为InternalBufferSize属性(用于监视网络上的目录)设置的最大大小为64KB。
FileSystemWatcher的实例监控到文件(夹)的变化后,会触发相应的事件,其中文件(夹)的添加,删除和修改会分别触发Created,Deleted,Changed事件,文件(夹)重命名时触发OnRenamed事件。
然后,在熟悉了FileSystemWatcher类后,我们开始自己的程序编写。
实例化FileSystemWatcher类,并传入需要监控的目录路径,以及是否制定监控的文件类型(文章前面有所介绍)。
_watcher=newFileSystemWatcher(_path,_filter);
注册监听事件,以及编写事件触发后相关的处理逻辑。
_watcher.Created+=newFileSystemEventHandler(OnChanged);
_watcher.Changed+=newFileSystemEventHandler(OnChanged);
_watcher.Deleted+=newFileSystemEventHandler(OnChanged);
_watcher.Renamed+=newRenamedEventHandler(OnRenamed);
_watcher.IncludeSubdirectories=true;
_watcher.EnableRaisingEvents=true;
在本程序中,专门定义了一个FileChangeInformation类来记录文件变化信息,并定义了一个CustomQueue类,该类类似于Queue类,是一个数据先进先出的集合,用来存储所有的文件变化消息,并提供数据持久化功能。
监控类-FileWatcher,代码如下:
///<summary> ///<summary> ///<summary> ///<summary> ///<summary> ///<summary> if(string.IsNullOrEmpty(_filter)) ///<summary> ///<summary> ///<summary> //文件夹的变化,只针对创建,重命名和删除动作,修改不做任何操作。 changeType=FileChangeType.Change; //创建消息,并压入队列中 ///<summary> 最后,功能调用如下: FileChangeInformationfci=null; 该程序实现了对文件目录下所有子目录和子文件的变化进行监控,并可通过FileChangeQueue属性访问文件变更消息,同时也可以设置其是否需要将数据持久化到磁盘文件中。
///<summary>
///文件监控类,用于监控指定目录下文件以及文件夹的变化
///</summary>
publicclassFileWatcher
{
privateFileSystemWatcher_watcher=null;
privatestring_path=string.Empty;
privatestring_filter=string.Empty;
privatebool_isWatch=false;
privateCustomQueue<FileChangeInformation>_queue=null;
///监控是否正在运行
///</summary>
publicboolIsWatch
{
get
{
return_isWatch;
}
}
///文件变更信息队列
///</summary>
publicCustomQueue<FileChangeInformation>FileChangeQueue
{
get
{
return_queue;
}
}
///初始化FileWatcher类
///</summary>
///<paramname="path">监控路径</param>
publicFileWatcher(stringpath)
{
_path=path;
_queue=newCustomQueue<FileChangeInformation>();
}
///<summary>
///初始化FileWatcher类,并指定是否持久化文件变更消息
///</summary>
///<paramname="path">监控路径</param>
///<paramname="isPersistence">是否持久化变更消息</param>
///<paramname="persistenceFilePath">持久化保存路径</param>
publicFileWatcher(stringpath,boolisPersistence,stringpersistenceFilePath)
{
_path=path;
_queue=newCustomQueue<FileChangeInformation>(isPersistence,persistenceFilePath);
}
///初始化FileWatcher类,并指定是否监控指定类型文件
///</summary>
///<paramname="path">监控路径</param>
///<paramname="filter">指定类型文件,格式如:*.txt,*.doc,*.rar</param>
publicFileWatcher(stringpath,stringfilter)
{
_path=path;
_filter=filter;
_queue=newCustomQueue<FileChangeInformation>();
}
///初始化FileWatcher类,并指定是否监控指定类型文件,是否持久化文件变更消息
///</summary>
///<paramname="path">监控路径</param>
///<paramname="filter">指定类型文件,格式如:*.txt,*.doc,*.rar</param>
///<paramname="isPersistence">是否持久化变更消息</param>
///<paramname="persistenceFilePath">持久化保存路径</param>
publicFileWatcher(stringpath,stringfilter,boolisPersistence,stringpersistenceFilePath)
{
_path=path;
_filter=filter;
_queue=newCustomQueue<FileChangeInformation>(isPersistence,persistenceFilePath);
}
///打开文件监听器
///</summary>
publicvoidOpen()
{
if(!Directory.Exists(_path))
{
Directory.CreateDirectory(_path);
}
{
_watcher=newFileSystemWatcher(_path);
}
else
{
_watcher=newFileSystemWatcher(_path,_filter);
}
//注册监听事件
_watcher.Created+=newFileSystemEventHandler(OnProcess);
_watcher.Changed+=newFileSystemEventHandler(OnProcess);
_watcher.Deleted+=newFileSystemEventHandler(OnProcess);
_watcher.Renamed+=newRenamedEventHandler(OnFileRenamed);
_watcher.IncludeSubdirectories=true;
_watcher.EnableRaisingEvents=true;
_isWatch=true;
}
///关闭监听器
///</summary>
publicvoidClose()
{
_isWatch=false;
_watcher.Created-=newFileSystemEventHandler(OnProcess);
_watcher.Changed-=newFileSystemEventHandler(OnProcess);
_watcher.Deleted-=newFileSystemEventHandler(OnProcess);
_watcher.Renamed-=newRenamedEventHandler(OnFileRenamed);
_watcher.EnableRaisingEvents=false;
_watcher=null;
}
///获取一条文件变更消息
///</summary>
///<returns></returns>
publicFileChangeInformationGet()
{
FileChangeInformationinfo=null;
if(_queue.Count>0)
{
lock(_queue)
{
info=_queue.Dequeue();
}
}
returninfo;
}
///监听事件触发的方法
///</summary>
///<paramname="sender"></param>
///<paramname="e"></param>
privatevoidOnProcess(objectsender,FileSystemEventArgse)
{
try
{
FileChangeTypechangeType=FileChangeType.Unknow;
if(e.ChangeType==WatcherChangeTypes.Created)
{
if(File.GetAttributes(e.FullPath)==FileAttributes.Directory)
{
changeType=FileChangeType.NewFolder;
}
else
{
changeType=FileChangeType.NewFile;
}
}
elseif(e.ChangeType==WatcherChangeTypes.Changed)
{
//部分文件创建时同样触发文件变化事件,此时记录变化操作没有意义
//如果
if(_queue.SelectAll(
delegate(FileChangeInformationfcm)
{
returnfcm.NewPath==e.FullPath&&fcm.ChangeType==FileChangeType.Change;
}).Count<FileChangeInformation>()>0)
{
return;
}
//因为文件夹下任何变化同样会触发文件的修改操作,没有任何意义.
if(File.GetAttributes(e.FullPath)==FileAttributes.Directory)
{
return;
}
}
elseif(e.ChangeType==WatcherChangeTypes.Deleted)
{
changeType=FileChangeType.Delete;
}
FileChangeInformationinfo=newFileChangeInformation(Guid.NewGuid().ToString(),changeType,e.FullPath,e.FullPath,e.Name,e.Name);
_queue.Enqueue(info);
}
catch
{
Close();
}
}
///文件或目录重命名时触发的事件
///</summary>
///<paramname="sender"></param>
///<paramname="e"></param>
privatevoidOnFileRenamed(objectsender,RenamedEventArgse)
{
try
{
//创建消息,并压入队列中
FileChangeInformationinfo=newFileChangeInformation(Guid.NewGuid().ToString(),FileChangeType.Rename,e.OldFullPath,e.FullPath,e.OldName,e.Name);
_queue.Enqueue(info);
}
catch
{
Close();
}
}
}
//初始化监控器
FileWatcherwatcher=newFileWatcher(@"D:\");
watcher.Open();
//获取消息
while(true)
{
//如果IsWatch为False,则可能监控内部发生异常终止了监控,需要重新开启监控
if(watcher.IsWatch)
{
//队列顶端的变更消息
fci=watcher.Get();
//处理消息的代码
//Print(fci);
}
else
{
watcher.Open();
}
Thread.Sleep(1000);
}相关文章