/// <summary> /// Called when [changed event fire]. /// </summary> /// <param name="source">The source.</param> /// <param name="e">The <see cref="FileSystemEventArgs"/> instance containing the event data.</param> private async void OnChanged(object source, FileSystemEventArgs e) { try { _iLog.Info("Recived the file into incoming folder."); bool isFileProcessed = false; var isReady = false; while (!isReady) { isReady = FileWatcherHelper.IsFileReady(e.FullPath); if (isReady) { isFileProcessed = await _fileProcessor.IsProcessed(e.FullPath); break; } } if (isFileProcessed) { _iLog.Info("Report generated successfully!!"); } } catch (Exception ex) { _iLog.Error($"Error while processing the file. Error: {ex.Message}"); } }
/// <inheritdoc /> public void Run() { var pathsForSetting = new ConcurrentBag <FileWatcherHelper>(); Parallel.ForEach(_listFromFileSystem.Value, name => { var file = new FileInfo(name); var fileWatcherHelper = new FileWatcherHelper { Path = name, LastWriteTime = file.LastWriteTime }; pathsForSetting.Add(fileWatcherHelper); }); var json = JsonConvert.SerializeObject(pathsForSetting, Formatting.Indented); if (File.Exists(_app.JsonPath)) { File.Delete(_app.JsonPath); } File.AppendAllText(_app.JsonPath, json); }
/// <summary> /// Deletes the local files. /// </summary> /// <param name="incomingFolderFilePath">The incoming folder file path.</param> private void ArchiveIncomingFile(string incomingFolderFilePath) { if (string.IsNullOrWhiteSpace(incomingFolderFilePath)) { return; } try { _iLog.Info("Archive file process started."); var fileNameWithoutExtension = Path.GetFileNameWithoutExtension(incomingFolderFilePath); var directoryName = Path.GetDirectoryName(incomingFolderFilePath); var files = Directory.GetFiles(directoryName, fileNameWithoutExtension + "*.*", SearchOption.TopDirectoryOnly); foreach (var item in files) { File.Copy(item, Path.Combine(_archiveIncomingFolderPath, Path.GetFileName(item)?.Replace(fileNameWithoutExtension, $"{fileNameWithoutExtension}_{FileWatcherHelper.GenerateShortGUID()}")), true); File.Delete(item); _iLog.Info("Incoming file archived successfully."); } } catch (Exception ex) { _iLog.Error($"Error file archiving the incoming file. Error: {ex.Message}"); } }
/// <summary> /// </summary> /// <exception cref="AggregateException">The exception that contains all the individual exceptions thrown on all threads.</exception> /// <exception cref="UnauthorizedAccessException">Access is denied. </exception> /// <exception cref="DirectoryNotFoundException">The specified path is invalid (for example, it is on an unmapped drive). </exception> /// <exception cref="IOException"> /// path includes an incorrect or invalid syntax for file name, directory name, or volume label syntax. /// </exception> /// <exception cref="SecurityException">The caller does not have the required permission. </exception> public void Write() { var pathsForSetting = new ConcurrentBag<FileWatcherHelper>(); Parallel.ForEach(ListFromFileSystem(), name => { var file = new FileInfo(name); var fileWatcherHelper = new FileWatcherHelper { Path = name, LastWriteTime = file.LastWriteTime }; pathsForSetting.Add(fileWatcherHelper); }); StreamWriter streamWriter = null; try { var serialiser = new XmlSerializer(typeof(List<FileWatcherHelper>)); streamWriter = new StreamWriter(_xmlPath); serialiser.Serialize(streamWriter, pathsForSetting.ToList()); } finally { streamWriter?.Close(); } }
private List<FileWatcherHelper> ConvertPathsFromSettingToDictionary() { StreamReader streamReader = null; try { var serialiser = new XmlSerializer(typeof(List<FileWatcherHelper>)); var paths = new ConcurrentBag<FileWatcherHelper>(); streamReader = new StreamReader(_xmlPath); var pathsFromSetting = (List<FileWatcherHelper>) serialiser.Deserialize(streamReader); Parallel.ForEach(pathsFromSetting, pathFromSetting => { var fileWatcherHelper = new FileWatcherHelper { Path = pathFromSetting.Path, LastWriteTime = pathFromSetting.LastWriteTime }; paths.Add(fileWatcherHelper); }); return paths.ToList(); } finally { streamReader?.Close(); } }