示例#1
0
        public static WatcherEvent Pick(List<FileSystemEventWrapper> events)
        {
            WatcherEvent result = null;

            if (events.Count > 0)
            {
                var @event = events[0];

                // waiting for follow up events
                if ((DateTime.Now - @event.Time).TotalMilliseconds < 100)
                {
                    return null;
                }

                // ignore "create new empty folder" event
                if (@event.Type == WatcherChangeTypes.Created
                    && Directory.Exists(@event.FullPath))
                {
                    events.RemoveAt(0);
                    return null;
                }

                ConnectMovedEvent(events);

                var series = TryPickSeries(events);

                @event = series[0];
                var @lastEvent = series[series.Count - 1];

                result = new WatcherEvent();
                result.Path = @event.Path;
                result.Time = @event.Time;

                switch (@lastEvent.Type)
                {
                    case WatcherChangeTypes.Created:
                    case WatcherChangeTypes.Changed:
                        {
                            result.Type = WatcherEventChangeTypes.Updated;
                            break;
                        }
                    case WatcherChangeTypes.Deleted:
                        {
                            result.Type = WatcherEventChangeTypes.Deleted;
                            break;
                        }
                    case WatcherChangeTypes.Renamed:
                        {
                            result.Type = WatcherEventChangeTypes.Moved;
                            result.NewPath = @lastEvent.NewPath;
                            break;
                        }
                    default:
                        {
                            throw new HDWatcherException("Unknow event type " + @lastEvent.Type.ToString());
                        }
                }

                return result;
            }

            return result;
        }
示例#2
0
        private static WatcherEvent TryPickDeleteEvent(List<FileSystemEventWrapper> events)
        {
            WatcherEvent result = null;
            string searchingPath = string.Empty;
            for (int i = events.Count; i >= 0; i--)
            {
                var @event = events[i];

                if (result == null)
                {
                    if (@event.Type == WatcherChangeTypes.Deleted)
                    {
                        result = new WatcherEvent();
                        result.Path = @event.Path;
                        result.Type = WatcherEventChangeTypes.Deleted;
                        result.Time = @event.Time;
                        searchingPath = result.Path;
                    }
                }
                else
                {
                    if (@event.Type == WatcherChangeTypes.Renamed)
                    {
                        if (searchingPath.Equals(@event.NewPath, StringComparison.OrdinalIgnoreCase))
                        {
                            searchingPath = @event.Path;
                            events.RemoveAt(i);
                        }
                    }
                    else
                    {
                        if (searchingPath.Equals(@event.Path, StringComparison.OrdinalIgnoreCase))
                        {
                            events.RemoveAt(i);
                        }
                    }
                }
            }
            return result;
        }