public IEnhancedFileSystemWatcher CreateEnhancedFileSystemWatcher(string folderToWatch, string includeFilter, string excludeFiler, int pollTime, bool includeSubdirectories, object userState) { IEnhancedFileSystemWatcher folderMonitor = new EnhancedFileSystemWatcher(folderToWatch, includeFilter, excludeFiler, pollTime, includeSubdirectories) { UserState = userState }; return folderMonitor; }
public void WatchStaticMappings([CanBeNull] string folder = null) { if (folder == null) { folder = _settings.FileSystemHandler.GetMappingFolder(); } if (!_settings.FileSystemHandler.FolderExists(folder)) { return; } bool includeSubdirectories = _settings.WatchStaticMappingsInSubdirectories == true; string includeSubdirectoriesText = includeSubdirectories ? " and Subdirectories" : string.Empty; _settings.Logger.Info($"Watching folder '{folder}'{includeSubdirectoriesText} for new, updated and deleted MappingFiles."); var watcher = new EnhancedFileSystemWatcher(folder, "*.json", EnhancedFileSystemWatcherTimeoutMs) { IncludeSubdirectories = includeSubdirectories }; watcher.Created += (sender, args) => { _settings.Logger.Info("MappingFile created : '{0}', reading file.", args.FullPath); if (!ReadStaticMappingAndAddOrUpdate(args.FullPath)) { _settings.Logger.Error("Unable to read MappingFile '{0}'.", args.FullPath); } }; watcher.Changed += (sender, args) => { _settings.Logger.Info("MappingFile updated : '{0}', reading file.", args.FullPath); if (!ReadStaticMappingAndAddOrUpdate(args.FullPath)) { _settings.Logger.Error("Unable to read MappingFile '{0}'.", args.FullPath); } }; watcher.Deleted += (sender, args) => { _settings.Logger.Info("MappingFile deleted : '{0}'", args.FullPath); string filenameWithoutExtension = Path.GetFileNameWithoutExtension(args.FullPath); if (Guid.TryParse(filenameWithoutExtension, out Guid guidFromFilename)) { DeleteMapping(guidFromFilename); } else { DeleteMapping(args.FullPath); } }; watcher.EnableRaisingEvents = true; }
static void Main(string[] args) { var fsw = new EnhancedFileSystemWatcher(@"C:\temp", "*.txt"); fsw.Created += fsw_Created; fsw.Changed += fsw_Changed; fsw.Deleted += fsw_Deleted; fsw.Renamed += fsw_Renamed; fsw.EnableRaisingEvents = true; Console.WriteLine("Waiting for things to happen in C:\\temp"); Console.ReadLine(); }
public void WatchStaticMappings([CanBeNull] string folder = null) { if (folder == null) { folder = Path.Combine(Directory.GetCurrentDirectory(), AdminMappingsFolder); } if (!Directory.Exists(folder)) { return; } _logger.Info("Watching folder '{0}' for new, updated and deleted MappingFiles.", folder); var watcher = new EnhancedFileSystemWatcher(folder, "*.json", 1000); watcher.Created += (sender, args) => { _logger.Info("New MappingFile created : '{0}'", args.FullPath); ReadStaticMappingAndAddOrUpdate(args.FullPath); }; watcher.Changed += (sender, args) => { _logger.Info("New MappingFile updated : '{0}'", args.FullPath); ReadStaticMappingAndAddOrUpdate(args.FullPath); }; watcher.Deleted += (sender, args) => { _logger.Info("New MappingFile deleted : '{0}'", args.FullPath); string filenameWithoutExtension = Path.GetFileNameWithoutExtension(args.FullPath); if (Guid.TryParse(filenameWithoutExtension, out Guid guidFromFilename)) { DeleteMapping(guidFromFilename); } else { DeleteMapping(args.FullPath); } }; watcher.EnableRaisingEvents = true; }
public void WatchStaticMappings([CanBeNull] string folder = null) { if (folder == null) { folder = Path.Combine(Directory.GetCurrentDirectory(), AdminMappingsFolder); } if (!Directory.Exists(folder)) { _logger.Warn("Mapping folder '{0}' does NOT exist. Will try creating.", folder); try { Directory.CreateDirectory(folder); } catch (Exception e) { _logger.Warn("Failed creation of mapping folder '{0}' with Exception: '{1}'", folder, e.Message); return; } } _logger.Debug("Attempting to create watcher for folder '{0}' for new, updated, deleted and renamed MappingFiles.", folder); _watcher = new EnhancedFileSystemWatcher(folder, "*.json", 1000); _watcher.Created += (sender, args) => { _logger.Info("MappingFile created : '{0}'", args.FullPath); ReadStaticMappingAndAddOrUpdate(args.FullPath); }; _watcher.Changed += (sender, args) => { _logger.Info("MappingFile updated : '{0}'", args.FullPath); ReadStaticMappingAndAddOrUpdate(args.FullPath); }; _watcher.Deleted += (sender, args) => { _logger.Info("MappingFile deleted : '{0}'", args.FullPath); string filenameWithoutExtension = Path.GetFileNameWithoutExtension(args.FullPath); if (Guid.TryParse(filenameWithoutExtension, out Guid guidFromFilename)) { DeleteMapping(guidFromFilename); } else { DeleteMapping(args.FullPath); } }; _watcher.Renamed += (sender, args) => { _logger.Info("MappingFile Renamed from : '{0}' to : '{1}'. Updating mapping.", args.OldFullPath, args.FullPath); try { ReadStaticMappingAndAddOrUpdate(args.FullPath); } catch (System.IO.FileNotFoundException e) { _logger.Info("MappingFile Renamed NOT found, ignoring (may be temporary file) : '{1}'. Ignoring mapping.", args.OldFullPath, args.FullPath); } }; _watcher.EnableRaisingEvents = true; _logger.Info("Watching folder '{0}' for new, updated, deleted and renamed MappingFiles.", folder); }