Exemplo n.º 1
0
        /// <summary>
        /// 监听事件触发的方法
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void OnProcess(object sender, FileSystemEventArgs e)
        {
            try
            {
                FileChangeType changeType = FileChangeType.Unknow;
                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();
            }
        }
Exemplo n.º 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();
     }
 }