コード例 #1
0
ファイル: HDWatcherEventPicker.cs プロジェクト: xfm84/test
        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
ファイル: HDWatcherEventPicker.cs プロジェクト: xfm84/test
        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;
        }