监视文件变化的类
2018-01-07 13:02
204 查看
using System; using System.Collections.Generic; using System.IO; using System.ComponentModel; namespaceFramework.Common { /// <summary> /// 监视文件变化的类,包括创建、修改、删除等操作的辅助类 /// <code> /// MyFileSystemWatcher fsw = new MyFileSystemWatcher(@"C:\Test", "Test.txt"); /// fsw.Created += new System.IO.FileSystemEventHandler(fsw_Created); /// fsw.Changed += new System.IO.FileSystemEventHandler(fsw_Changed); /// fsw.Deleted += new System.IO.FileSystemEventHandler(fsw_Deleted); /// fsw.Renamed += new System.IO.RenamedEventHandler(fsw_Renamed); /// fsw.EnableRaisingEvents = true; /// </code> /// </summary> publicclass MyFileSystemWatcher : FileSystemWatcher, IDisposable { #region Private Members // This Dictionary keeps the track of when an event occured last for a particular file privateDictionary<string, datetime=""> _lastFileEvent; // Interval in Millisecond privateint _interval; //Timespan created when interval is set privateTimeSpan _recentTimeSpan; #endregion #region Constructors /// <summary> /// 构造函数,调用基类构造函数并初始化成员 /// </summary> publicMyFileSystemWatcher() : base() { InitializeMembers(); } /// <summary> /// 指定路径进行构造 /// </summary> /// <param name="Path">文件路径 publicMyFileSystemWatcher(stringPath) : base(Path) { InitializeMembers(); } /// <summary> /// 指定路径和过滤器进行构造 /// </summary> /// <param name="Path">文件或文件夹路径 /// <param name="Filter">过滤器 publicMyFileSystemWatcher(stringPath, stringFilter) : base(Path, Filter) { InitializeMembers(); } #endregion #region Events // These events hide the events from the base class. // We want to raise these events appropriately and we do not want the // users of this class subscribing to these events of the base class accidentally /// <summary> /// 文件系统发生变化 /// </summary> publicnew eventFileSystemEventHandler Changed; /// <summary> /// 文件创建事件 /// </summary> publicnew eventFileSystemEventHandler Created; /// <summary> /// 文件删除事件 /// </summary> publicnew eventFileSystemEventHandler Deleted; /// <summary> /// 文件重命名事件 /// </summary> publicnew eventRenamedEventHandler Renamed; #endregion #region Protected Methods /// <summary> /// 文件发生变化 /// </summary> protectednew virtualvoid OnChanged(FileSystemEventArgs e) { if(Changed != null) Changed(this, e); } /// <summary> /// 文件创建后 /// </summary> protectednew virtualvoid OnCreated(FileSystemEventArgs e) { if(Created != null) Created(this, e); } /// <summary> /// 文件删除后 /// </summary> protectednew virtualvoid OnDeleted(FileSystemEventArgs e) { if(Deleted != null) Deleted(this, e); } /// <summary> /// 文件重命名后 /// </summary> protectednew virtualvoid OnRenamed(RenamedEventArgs e) { if(Renamed != null) Renamed(this, e); } #endregion #region Private Methods /// <summary> /// This Method Initializes the private members. /// Interval is set to its default value of 100 millisecond /// FilterRecentEvents is set to true, _lastFileEvent dictionary is initialized /// We subscribe to the base class events. /// </summary> privatevoid InitializeMembers() { Interval = 100; FilterRecentEvents =true; _lastFileEvent =new Dictionary<string, datetime="">(); base.Created +=new FileSystemEventHandler(OnCreated); base.Changed +=new FileSystemEventHandler(OnChanged); base.Deleted +=new FileSystemEventHandler(OnDeleted); base.Renamed +=new RenamedEventHandler(OnRenamed); } /// <summary> /// This method searches the dictionary to find out when the last event occured /// for a particular file. If that event occured within the specified timespan /// it returns true, else false /// </summary> /// <param name="FileName">The filename to be checked /// <returns>True if an event has occured within the specified interval, False otherwise</returns> privatebool HasAnotherFileEventOccuredRecently(stringFileName) { boolretVal = false; // Check dictionary only if user wants to filter recent events otherwise return Value stays False if(FilterRecentEvents) { if(_lastFileEvent.ContainsKey(FileName)) { // If dictionary contains the filename, check how much time has elapsed // since the last event occured. If the timespan is less that the // specified interval, set return value to true // and store current datetime in dictionary for this file DateTime lastEventTime = _lastFileEvent[FileName]; DateTime currentTime = DateTime.Now; TimeSpan timeSinceLastEvent = currentTime - lastEventTime; retVal = timeSinceLastEvent < _recentTimeSpan; _lastFileEvent[FileName] = currentTime; } else { // If dictionary does not contain the filename, // no event has occured in past for this file, so set return value to false // and annd filename alongwith current datetime to the dictionary _lastFileEvent.Add(FileName, DateTime.Now); retVal =false; } } returnretVal; } #region FileSystemWatcher EventHandlers // Base class Event Handlers. Check if an event has occured recently and call method // to raise appropriate event only if no recent event is detected privatevoid OnChanged(objectsender, FileSystemEventArgs e) { if(!HasAnotherFileEventOccuredRecently(e.FullPath)) this.OnChanged(e); } privatevoid OnCreated(objectsender, FileSystemEventArgs e) { if(!HasAnotherFileEventOccuredRecently(e.FullPath)) this.OnCreated(e); } privatevoid OnDeleted(objectsender, FileSystemEventArgs e) { if(!HasAnotherFileEventOccuredRecently(e.FullPath)) this.OnDeleted(e); } privatevoid OnRenamed(objectsender, RenamedEventArgs e) { if(!HasAnotherFileEventOccuredRecently(e.OldFullPath)) this.OnRenamed(e); } #endregion #endregion #region Public Properties /// <summary> /// 时间间隔,以毫秒为单位,在其中的事件被认为是“最近” /// </summary> publicint Interval { get { return_interval; } set { _interval = value; // Set timespan based on the value passed _recentTimeSpan =new TimeSpan(0, 0, 0, 0, value); } } /// <summary> /// 允许用户设置是否过滤最近发生的事件。如果此设为false,该类的行为就像System.IO.FileSystemWatcher类 /// </summary> publicbool FilterRecentEvents {get; set; } #endregion #region IDisposable Members /// <summary> /// 释放资源 4000 /// </summary> publicnew void Dispose() { base.Dispose(); } #endregion } }
相关文章推荐
- c#使用filesystemwatcher监视文件系统的变化
- 【转】c#如何监视文件或者文件夹的变化
- 文件变化监视 java.nio.file.WatchService
- .net 监视文件系统中目录和文件的变化
- Android 监视文件或目录变化
- 用inotify监视文件/目录变化
- 用python监视文件变化
- 用inotify监视文件/目录变化
- 监视文件变化
- 使用控制线程监视目录文件变化
- 监视资源管理器的文件变化
- 使用FileSystemWatcher监视文件变化
- SQL Server 监视数据文件大小变化
- Android 监视文件或目录变化
- Node.js文件模块fs监视文件变化
- c#如何监视文件或者文件夹的变化
- Android 监视文件或目录变化
- FindFirstChangeNotification,创建一个文件通知对象,该对象用于监视文件系统发生的变化
- tails 指定输出文件的行 监视文件的变化
- c#的FileSystemWatcher对象监视文件的变化的事件,无休止的触发事件的解决办法