How to implement a file manager using Reactive extensions

I am new to Rx and want to use it in my current project. I am trying to implement a file viewer system. At least for now, I'm only interested in the file creation event. But when I try to execute the code below, I get "The value cannot be an error message." Please help me with the code below.

class Program { static void Main(string[] args) { IDisposable writer = new FileSystemObservable(@"D:\Code\Rx\Dropbox\", "*.*", false) .CreatedFiles .Where(x => (new FileInfo(x.FullPath)).Length > 0) .Select(x => x.Name) .Subscribe(Console.WriteLine); Console.ReadLine(); } } class FileSystemObservable { private readonly FileSystemWatcher fileSystemWatcher; public FileSystemObservable(string directory, string filter, bool includeSubdirectories) { fileSystemWatcher = new FileSystemWatcher(directory, filter) { EnableRaisingEvents = true, IncludeSubdirectories = includeSubdirectories }; CreatedFiles = Observable.FromEventPattern<FileSystemEventHandler, FileSystemEventArgs> (h => fileSystemWatcher.Created += h, h => fileSystemWatcher.Created -= h) .Select(x => new { x.EventArgs }) as IObservable<FileSystemEventArgs>; Errors = Observable.FromEventPattern<ErrorEventHandler, ErrorEventArgs> (h => fileSystemWatcher.Error += h, h => fileSystemWatcher.Error -= h) .Select(x => new { x.EventArgs }) as IObservable<ErrorEventArgs>; } public IObservable<ErrorEventArgs> Errors { get; private set; } public IObservable<FileSystemEventArgs> CreatedFiles { get; private set; } } 
+4
source share
1 answer

Result

 Select(x => new { x.EventArgs }) as IObservable<ErrorEventArgs> 

and

 .Select(x => new { x.EventArgs }) as IObservable<FileSystemEventArgs>; 
Lines

will always return null.

The type Select(x => new { x.EventArgs }) is equal to IObservable<'a> , where 'a is some anonymous type.

Instead, you should use:

 CreatedFiles = Observable.FromEventPattern<FileSystemEventHandler, FileSystemEventArgs>( h => fileSystemWatcher.Created += h, h => fileSystemWatcher.Created -= h) .Select(x => x.EventArgs); Errors = Observable.FromEventPattern<ErrorEventHandler, ErrorEventArgs>( h => fileSystemWatcher.Error += h, h => fileSystemWatcher.Error -= h) .Select(x => x.EventArgs); 
+5
source

All Articles