private void OnChange(object sender, FileSystemEventArgs e) { // make sure the path exist on the file system. Changes can't be made to paths that don't exist if (File.Exists(e.FullPath) || Directory.Exists(e.FullPath)) { // check if the path exist in the _existingPaths collection if (!_existingPaths.Contains(e.FullPath)) { return; } FileSystemObserverKey key = new FileSystemObserverKey() { Name = new FileInfo(e.Name).Name, FullPath = e.FullPath, EventType = FileSystemObserverEventType.ChangedEventType }; FileSystemObserverValue value = new FileSystemObserverValue() { EventType = FileSystemObserverEventType.ChangedEventType, Timestamp = DateTime.Now }; OnFileSystemObserverEvent(key, value); } }
private void FireChangedEvent(FileSystemObserverKey key) { FileSystemEvent evt = ChangedEvent; if (evt != null) { // make sure the path still exist before throwing an event if (PathExists(key.FullPath) && CheckIfWeWantToFireThisEvent(key.FullPath)) { // okay you can fire the Changed event now evt(key.FullPath); } } }
private void OnFileSystemObserverEvent(FileSystemObserverKey key, FileSystemObserverValue value) { // common method for adding events to pendingEvents collection from the On event handlers // block other threads from access pending events while processing lock (_pendingEvents) { // save most recent event _pendingEvents[key] = value; // start timer if not already started if (!_timerStarted) { _timer.Change(100, 100); _timerStarted = true; } } }
private void FireRenamedEvent(FileSystemObserverKey key) { FileSystemRenameEvent evt = RenamedEvent; if (evt != null) { // can't rename to an existing path if (!_existingPaths.Contains(key.FullPath) && CheckIfWeWantToFireThisEvent(key.FullPath)) { // replace the renamed path _existingPaths.Remove(key.OldFullPath); _existingPaths.Add(key.FullPath); // okay you can fire the Renamed event now evt(key.OldFullPath, key.OldName, key.FullPath, key.Name); } } }
private void OnRename(object sender, RenamedEventArgs e) { FileSystemObserverKey key = new FileSystemObserverKey() { OldFullPath = e.OldFullPath, OldName = new FileInfo(e.OldName).Name, FullPath = e.FullPath, Name = new FileInfo(e.Name).Name, EventType = FileSystemObserverEventType.RenamedEventType }; FileSystemObserverValue value = new FileSystemObserverValue() { EventType = FileSystemObserverEventType.RenamedEventType, Timestamp = DateTime.Now }; OnFileSystemObserverEvent(key, value); }
private void FireDeletedEvent(FileSystemObserverKey key) { FileSystemEvent evt = DeletedEvent; if (evt != null) { // if the Path still exist on the fileSystem then it wasn't deleted // this can happen during the saving file process. // Don't thrown this event unless the path doesn't exist if (!PathExists(key.FullPath) && CheckIfWeWantToFireThisEvent(key.FullPath)) { // remove the path from existing paths _existingPaths.Remove(key.FullPath); // okay you can fire the Deleted event now evt(key.FullPath); } } }
private void OnDelete(object sender, FileSystemEventArgs e) { // check if the path exist in the _existingPaths collection if (_existingPaths.Contains(e.FullPath)) { FileSystemObserverKey key = new FileSystemObserverKey() { Name = new FileInfo(e.Name).Name, FullPath = e.FullPath, EventType = FileSystemObserverEventType.DeletedEventType }; FileSystemObserverValue value = new FileSystemObserverValue() { EventType = FileSystemObserverEventType.DeletedEventType, Timestamp = DateTime.Now }; OnFileSystemObserverEvent(key, value); } }
private void FireCreatedEvent(FileSystemObserverKey key) { FileSystemEvent evt = CreatedEvent; if (evt != null) { // check if the path still exist before firing this created event. this should filter out some temp files if (PathExists(key.FullPath) && CheckIfWeWantToFireThisEvent(key.FullPath)) { // check if path already exist. Can't create a path that already exist. if (!_existingPaths.Contains(key.FullPath)) { // need to add this to the existing paths list _existingPaths.Add(key.FullPath); // okay you can fire the Created event now evt(key.FullPath); } } } }