public FileWatchInfo Subscribe(string path, bool includeSubDirectories, FileEventHandler eventHandler, IEnumerable <string> filter, IEnumerable <FileWatchChangeType> changeTypes) { var id = GetFirstFreeId(); var fileWatcherInfo = new FileWatcherInfo(id, path, includeSubDirectories, eventHandler, filter, changeTypes); return(Subscribe(fileWatcherInfo)); }
/// <summary> /// Adds a new <see cref="FileWatcherInfo"/> to the current <see cref="FileWatcher"/>. /// The <see cref="FileEventHandler"/> in the specified <see cref="fileWatcherInfo"/> will be called on a change in the current watch. /// </summary> /// <exception cref="InvalidFileWatchInfoException"> /// The given <see cref="fileWatcherInfo"/> contains a different path than the one being watched. /// </exception> /// <param name="fileWatcherInfo">The <see cref="fileWatcherInfo"/> to add.</param> public void AddSubscription(FileWatcherInfo fileWatcherInfo) { if (!_watchedPath.IsEquivalentPath(fileWatcherInfo.Path)) { throw new InvalidFileWatchInfoException("The specified path does not equal the watched path."); } if (!_subscriptions.TryAdd(fileWatcherInfo, DateTime.Now)) { return; // The given subscription already exists } if (fileWatcherInfo.EventHandler == null) { return; // No event to call } var eventArgs = new FileWatchEventArgs(_watching ? FileWatchChangeType.Enabled : FileWatchChangeType.Disabled, fileWatcherInfo.Path); RaiseEvent(new EventData(fileWatcherInfo, eventArgs)); }
// ToDo: subscribe to parent path if available and adjust filter // eg if D:\ is watched, D:\TEMP can be added there // Set a SubscribedPath property for FileWatcherInfo. // Also change the way FileWatcherInfo applies filters! Must inspect the path! private FileWatcherInfo Subscribe(FileWatcherInfo fileWatcherInfo) { lock (_watchers) { bool foundValidWatcher = false; // Indicates whether we found a useful FileWatcher during the foreach. foreach (var item in _watchers) { // Does the current FileWatcher watch the specified path? if (item.Key == fileWatcherInfo.Path) { foundValidWatcher = !item.Value.IsDisposed; if (foundValidWatcher) { // The watcher is not disposed, add a subscription. _watchers[fileWatcherInfo.Path].AddSubscription(fileWatcherInfo); } else { // No need to keep this disposed watcher referenced. _watchers.Remove(item); } break; } } if (!foundValidWatcher) { // Existing non-disposed watcher is not found, create a new one. var watcher = new FileWatcher(fileWatcherInfo.Path); _watchers.Add(fileWatcherInfo.Path, watcher); watcher.Disposed += FileWatcher_Disposed; watcher.Watching = true; watcher.AddSubscription(fileWatcherInfo); } } return(fileWatcherInfo); }
/// <summary> /// Adds a new <see cref="FileWatcherInfo"/> to the current <see cref="FileWatcher"/>. /// The <see cref="FileEventHandler"/> in the specified <see cref="fileWatcherInfo"/> will be called on a change in the current watch. /// </summary> /// <exception cref="InvalidFileWatchInfoException"> /// The given <see cref="fileWatcherInfo"/> contains a different path than the one being watched. /// </exception> /// <param name="fileWatcherInfo">The <see cref="fileWatcherInfo"/> to add.</param> public void AddSubscription(FileWatcherInfo fileWatcherInfo) { if (fileWatcherInfo.Path != _watchedPath.Path.FullName) { throw new InvalidFileWatchInfoException("The specified path does not equal the watched path."); } lock (_subscriptions) { if (_subscriptions.Contains(fileWatcherInfo)) { return; // The given subscription already exists } _subscriptions.Add(fileWatcherInfo); if (fileWatcherInfo.EventHandler == null) { return; // No event to call } var eventArgs = new FileWatchEventArgs(_watching ? FileWatchChangeType.Enabled : FileWatchChangeType.Disabled, fileWatcherInfo.Path); RaiseEvent(new EventData(fileWatcherInfo, eventArgs)); } }
/// <summary> /// Removes the given FileWatcherInfo from the current watch. /// The eventhandler won't be called anymore on a change in the current watch. /// </summary> /// <param name="fileWatcherInfo">The FileWatcherInfo to remove.</param> /// <returns>True if the FileWatcherInfo is found and removed.</returns> public bool RemoveSubscription(FileWatcherInfo fileWatcherInfo) { lock (_subscriptions) return(_subscriptions.Remove(fileWatcherInfo)); }
/// <summary> /// Removes the given FileWatcherInfo from the current watch. /// The eventhandler won't be called anymore on a change in the current watch. /// </summary> /// <param name="fileWatcherInfo">The FileWatcherInfo to remove.</param> /// <returns>True if the FileWatcherInfo is found and removed.</returns> public bool RemoveSubscription(FileWatcherInfo fileWatcherInfo) { lock (_subscriptions) return _subscriptions.Remove(fileWatcherInfo); }
/// <summary> /// Adds a new <see cref="FileWatcherInfo"/> to the current <see cref="FileWatcher"/>. /// The <see cref="FileEventHandler"/> in the specified <see cref="fileWatcherInfo"/> will be called on a change in the current watch. /// </summary> /// <exception cref="InvalidFileWatchInfoException"> /// The given <see cref="fileWatcherInfo"/> contains a different path than the one being watched. /// </exception> /// <param name="fileWatcherInfo">The <see cref="fileWatcherInfo"/> to add.</param> public void AddSubscription(FileWatcherInfo fileWatcherInfo) { if (fileWatcherInfo.Path != _watchedPath.Path.FullName) throw new InvalidFileWatchInfoException("The specified path does not equal the watched path."); lock (_subscriptions) { if (_subscriptions.Contains(fileWatcherInfo)) return; // The given subscription already exists _subscriptions.Add(fileWatcherInfo); if (fileWatcherInfo.EventHandler == null) return; // No event to call var eventArgs = new FileWatchEventArgs(_watching ? FileWatchChangeType.Enabled : FileWatchChangeType.Disabled, fileWatcherInfo.Path); RaiseEvent(new EventData(fileWatcherInfo, eventArgs)); } }
public FileWatchInfo Subscribe(string path, bool includeSubDirectories, FileEventHandler eventHandler, IEnumerable<string> filter, IEnumerable<FileWatchChangeType> changeTypes) { var id = GetFirstFreeId(); var fileWatcherInfo = new FileWatcherInfo(id, path, includeSubDirectories, eventHandler, filter, changeTypes); return Subscribe(fileWatcherInfo); }
// ToDo: subscribe to parent path if available and adjust filter // eg if D:\ is watched, D:\TEMP can be added there // Set a SubscribedPath property for FileWatcherInfo. // Also change the way FileWatcherInfo applies filters! Must inspect the path! private FileWatcherInfo Subscribe(FileWatcherInfo fileWatcherInfo) { lock (_watchers) { bool foundValidWatcher = false; // Indicates whether we found a useful FileWatcher during the foreach. foreach (var item in _watchers) { // Does the current FileWatcher watch the specified path? if (item.Key == fileWatcherInfo.Path) { foundValidWatcher = !item.Value.IsDisposed; if (foundValidWatcher) // The watcher is not disposed, add a subscription. _watchers[fileWatcherInfo.Path].AddSubscription(fileWatcherInfo); else // No need to keep this disposed watcher referenced. _watchers.Remove(item); break; } } if (!foundValidWatcher) { // Existing non-disposed watcher is not found, create a new one. var watcher = new FileWatcher(fileWatcherInfo.Path); _watchers.Add(fileWatcherInfo.Path, watcher); watcher.Disposed += FileWatcher_Disposed; watcher.Watching = true; watcher.AddSubscription(fileWatcherInfo); } } return fileWatcherInfo; }
/// <summary> /// Removes the given FileWatcherInfo from the current watch. /// The eventhandler won't be called anymore on a change in the current watch. /// </summary> /// <param name="fileWatcherInfo">The FileWatcherInfo to remove.</param> /// <returns>True if the FileWatcherInfo is found and removed.</returns> public bool RemoveSubscription(FileWatcherInfo fileWatcherInfo) { return(_subscriptions.TryRemove(fileWatcherInfo, out _)); }