监视文件变化的类

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>
        /// 释放资源
        /// </summary>
        publicnew void Dispose()
        {
            base.Dispose();
        }
 
        #endregion
    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值