public void Add(string filePath, string identifier) { if (_filePathsToMonitor.ContainsKey(filePath)) { return; // file is already monitored } lock (_syncRoot) { _filePathsToMonitor.Add(filePath, identifier); } string folder = Path.GetDirectoryName(filePath); if (folder != null) { lock (_syncRoot) { if (_fileWatchers.ContainsKey(folder)) { return; // directory is already monitored } var fileSystemWatcher = new FileWatcher(folder, _filter, _throttle); SubscribeToFileChanges(fileSystemWatcher); if (IsMonitoring) { fileSystemWatcher.StartMonitoring(); } _fileWatchers.Add(folder, fileSystemWatcher); } SubscribeToEvents(); } }
private void SubscribeToFileChanges(FileWatcher fileWatcher) { IDisposable subscription = fileWatcher.Subscribe(FileHasChanged, FileMonitorGotAnError); _subscriptions.Add(fileWatcher.Path, subscription); }