Exemple #1
0
    protected override void Main()
    {
      TraceLine(Instructions.PressAnyKeyToCancel);

      string path = Environment.ExpandEnvironmentVariables(@"%systemdrive%\");

      using (var watcher = new FileSystemWatcher(path))
      {
        watcher.InternalBufferSize = 0;		// Not recommended; used here for testing only.
        watcher.IncludeSubdirectories = true;
        watcher.NotifyFilter =
            NotifyFilters.FileName
          | NotifyFilters.DirectoryName
          | NotifyFilters.Size
          | NotifyFilters.CreationTime
          | NotifyFilters.LastWrite
          | NotifyFilters.LastAccess
          | NotifyFilters.Attributes
          | NotifyFilters.Security;

        using (watcher
          .Watch(WatcherChangeTypes.All)
          .Subscribe(ConsoleOutput()))
        {
          WaitForKey();
        }
      }
    }
        internal static ReadOnlyListSubject <TResult> CollectInternal <TResult>(
            this FileSystemWatcher watcher,
            bool composited,
            Func <IObservable <CollectionNotification <string> >, IObservable <CollectionModification <TResult> > > selector,
            IScheduler scheduler)
        {
            Contract.Requires(watcher != null);
            Contract.Requires(selector != null);
            Contract.Requires(scheduler != null);
            Contract.Ensures(Contract.Result <ReadOnlyListSubject <TResult> >() != null);

            var existing = Directory
                           .EnumerateFiles(
                watcher.Path,
                watcher.Filter,
                watcher.IncludeSubdirectories ? SearchOption.AllDirectories : SearchOption.TopDirectoryOnly);

            var changes = watcher
                          .Watch(WatcherChangeTypes.Created | WatcherChangeTypes.Deleted | WatcherChangeTypes.Renamed)
                          .SelectMany(notification => notification.Change == WatcherChangeTypes.Created
          ? Observable.Return(CollectionModification.CreateAdd(notification.FullPath))
          : notification.Change == WatcherChangeTypes.Deleted
            ? Observable.Return(CollectionModification.CreateRemove(notification.FullPath))
            : Observable.Return(CollectionModification.CreateRemove(notification.OldFullPath)).Concat(Observable.Return(CollectionModification.CreateAdd(notification.FullPath))));

            if (composited)
            {
                changes = changes.Finally(watcher.Dispose);
            }

            return(existing.ToObservable(scheduler).Collect(changes.ObserveOn(scheduler), selector, StringComparer.OrdinalIgnoreCase));
        }