Example #1
0
        /// <summary>
        /// 监听事件触发的方法
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void OnProcess(object sender, FileSystemEventArgs e)
        {
            try
            {
                FileChangeType changeType = FileChangeType.Unknown;
                if (e.ChangeType == WatcherChangeTypes.Created)
                {
                    if (File.GetAttributes(e.FullPath) == FileAttributes.Directory)
                    {
                        changeType = FileChangeType.NewFolder;
                    }
                    else
                    {
                        changeType = FileChangeType.NewFile;
                    }
                }
                else if (e.ChangeType == WatcherChangeTypes.Changed)
                {
                    //部分文件创建时同样触发文件变化事件,此时记录变化操作没有意义
                    //如果
                    //if (_queue.SelectAll(
                    //    delegate (FileChangeInformation fcm)
                    //    {
                    //        return fcm.NewPath == e.FullPath && fcm.ChangeType == FileChangeType.Change;
                    //    }).Count<FileChangeInformation>() > 0)
                    //{
                    //    return;
                    //}

                    //文件夹的变化,只针对创建,重命名和删除动作,修改不做任何操作。
                    //因为文件夹下任何变化同样会触发文件的修改操作,没有任何意义.
                    if (File.GetAttributes(e.FullPath) == FileAttributes.Directory)
                    {
                        return;
                    }

                    changeType = FileChangeType.Change;
                }
                else if (e.ChangeType == WatcherChangeTypes.Deleted)
                {
                    changeType = FileChangeType.Delete;
                }

                //创建消息,并压入队列中
                FileChangeInformation info = new FileChangeInformation(Guid.NewGuid().ToString(), changeType, e.FullPath, e.FullPath, e.Name, e.Name);
                _queue.Enqueue(info);
            }
            catch
            {
                Close();
            }
        }
Example #2
0
 /// <summary>
 /// 文件或目录重命名时触发的事件
 /// </summary>
 /// <param name="sender"></param>
 /// <param name="e"></param>
 private void OnFileRenamed(object sender, RenamedEventArgs e)
 {
     try
     {
         //创建消息,并压入队列中
         FileChangeInformation info = new FileChangeInformation(Guid.NewGuid().ToString(), FileChangeType.Rename, e.OldFullPath, e.FullPath, e.OldName, e.Name);
         _queue.Enqueue(info);
     }
     catch
     {
         Close();
     }
 }
Example #3
0
        /// <summary>
        /// 获取一条文件变更消息
        /// </summary>
        /// <returns></returns>
        public FileChangeInformation Get()
        {
            FileChangeInformation info = null;

            if (_queue.Count > 0)
            {
                lock (_queue)
                {
                    _queue.TryDequeue(out info);
                }
            }
            return(info);
        }