public override string EvaluateEx(ParsedLine line) { if (line.IsCommand && line.Command.ToLower() == "unwatch") { if (line.Args.Length < 1) { return "!unwatch [path]"; } // Request that this path no longer be watched _unwatch.OnNext(line.Args[0]); return null; } if (!line.IsCommand || line.Command.ToLower() != "watch") { return null; } string help = "!watch [path]"; // Verify we have enough arguments if (line.Args.Length < 1) { return help; } string path = line.Args[0]; try { // Create a file system watcher for the requested folder var fsw = new FileSystemWatcher(path); fsw.EnableRaisingEvents = true; // Create observables for the Created, Deleted, and Renamed events from the file system watcher // Each one takes the events and converts them to a string description of the event var obsCreated = fsw.CreateObservableForCreated().Select(args => String.Format("{0} was created", args.Name)); var obsDeleted = fsw.CreateObservableForDeleted().Select(args => String.Format("{0} was deleted", args.Name)); var obsRenamed = fsw.CreateObservableForRenamed().Select(args => String.Format("{0} was renamed", args.Name)); var txt = Observable.Return(String.Format("Watching for changes to {0}.", path)); // Merge all the events into a single stream var changes = txt.Concat( obsCreated.Merge(obsDeleted).Merge(obsRenamed) .Merge(_unwatch.Where(s => s.Contains(path))) // Merge with the stream of unwatch requests which match this path .TakeWhile(args => args != path)); // end the sequence when we get a string that just matches the path (which would be from the unwatch) changes.Subscribe((msg) => { this.SendMessage(msg, line.From, BotMessageType.chat); }); return null; } catch (Exception ex) { // Show the error in the chat - most likely it'll be an invalid path issue return ex.Message; } }