/// <summary> /// Создает экземпляры <see cref="FileSystemWatcher"/> для указаных папок /// </summary> /// <param name="folderPaths">Список путей к папкам для прослушивания</param> /// <param name="handlers">Список обработчиков</param> private void CreateWatchers(ListenedFolderPathElementCollection folderPaths, List <FileSystemEventHandler> handlers) { if (folderPaths != null) { foreach (ListenedFolderPathElement folderPath in folderPaths) { try { FileSystemWatcher watcher = new FileSystemWatcher(folderPath.FolderPath); watcher.NotifyFilter = NotifyFilters.LastAccess | NotifyFilters.LastWrite | NotifyFilters.FileName | NotifyFilters.DirectoryName; //определяем обработчики события создания файла if (handlers != null) { foreach (var item in handlers) { watcher.Created += item; } } watcher.EnableRaisingEvents = true; watchers.Add(watcher); } catch (ArgumentException e) { loger.PrintError(e.Message); } } } }
private static void CreateDirectory(ListenedFolderPathElementCollection folders) { foreach (ListenedFolderPathElement folder in folders) { if (!Directory.Exists(folder.FolderPath)) { Directory.CreateDirectory(folder.FolderPath); } } }
private static void Initialize() { var configFile = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None); var configuration = (CustomConfigurationSection)configFile.Sections["customSection"]; CultureInfo culture = new CultureInfo(configuration.Culture.Culture); CultureInfo.DefaultThreadCurrentCulture = culture; CultureInfo.DefaultThreadCurrentUICulture = culture; CreateDirectory(configuration.Paths); listenedFolders = configuration.Paths; }
public MyWatcher(ListenedFolderPathElementCollection folderPaths, List <FileSystemEventHandler> handlers, ILoger loger) { this.loger = loger; CreateWatchers(folderPaths, handlers); }