Skip to content

Instantly share code, notes, and snippets.

@kstrauss
Last active August 29, 2015 14:05
Show Gist options
  • Save kstrauss/c5640251967239cc9ca9 to your computer and use it in GitHub Desktop.
Save kstrauss/c5640251967239cc9ca9 to your computer and use it in GitHub Desktop.
void Main()
{
var fsw = new FileSystemWatcher(@"c:\"){
IncludeSubdirectories = true
};
fsw.EnableRaisingEvents = true;
var created = Observable.FromEventPattern<FileSystemEventHandler, FileSystemEventArgs>(h=>fsw.Created+=h,h=>fsw.Created-=h);
var changed = Observable.FromEventPattern<FileSystemEventHandler, FileSystemEventArgs>(h=>fsw.Changed+=h,h=>fsw.Changed-=h);
var deleted = Observable.FromEventPattern<FileSystemEventHandler, FileSystemEventArgs>(h=>fsw.Deleted+=h,h=>fsw.Deleted-=h);
var renamed = Observable.FromEventPattern<RenamedEventHandler, FileSystemEventArgs>(h=>fsw.Renamed+=h,h=>fsw.Renamed-=h);
created.Merge(changed)
.Merge(deleted)
.Merge(renamed)
.Select(x=>x.EventArgs)
.Select(x=>String.Format("{1} was {0}",x.ChangeType,x.FullPath))
.DistinctUntilChanged()
// use linqpad's utility observer
.DumpLive();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment