private static void CreateACopyOfDocument(string fullPath, string filename, string oldName = "", bool isRenamed = false) { try { if (Path.GetExtension(filename).ToUpper() != ".TXT" && Path.GetExtension(filename).ToUpper() != ".DIP") { CustomFileHandling.WaitForFile(fullPath); string pathToMove = DeletedCopyFolder + GetBackupFolderName(fullPath, true); CustomFileHandling.CreateDirectoryIfDoesNotExist(pathToMove); if (isRenamed) { File.Copy(fullPath, pathToMove + "Renamed(" + Path.GetFileNameWithoutExtension(oldName) + ") to_" + filename); } else { File.Copy(fullPath, pathToMove + AppendTimeStamp(filename), true); } } } catch (Exception ex) { if (Logger.captureApplicationLogs) { Logger.AddtoWritingQueue.Enqueue("Application: " + DateTime.Now.ToString("MMddyyyy HH:mm:ss") + " Exception at Created Event \r\n" + ex.Message); } } }
/// <summary> /// Archiving processed folder. /// </summary> private static void ProcessedFolder() { string[] watchPathArray = System.Configuration.ConfigurationManager.AppSettings["WatchPaths"].ToString().Split('|'); foreach (string watchPath in watchPathArray) { string processedDirectory = watchPath + "PROCESSED\\"; if (Directory.Exists(processedDirectory)) { foreach (var fileInfo in new DirectoryInfo(processedDirectory).GetFiles().OrderBy(x => x.LastWriteTime)) { int totalNumberOfDays = (System.DateTime.Now - fileInfo.LastWriteTime).Days; if (totalNumberOfDays > Convert.ToInt32(ConfigurationManager.AppSettings["NumberOfDaysToKeepProcessedFile"].ToString().Trim())) { try { string folderName = watchPath + backupFolderName + "\\" + backupFolderPrefix + fileInfo.LastWriteTime.ToString("MM-yyyy") + "\\"; CustomFileHandling.CreateDirectoryIfDoesNotExist(folderName); CustomFileHandling.WaitForFile(fileInfo.FullName); File.Move(fileInfo.FullName, folderName + fileInfo.Name); } catch (Exception ex) { if (Logger.captureApplicationLogs) { Logger.AddtoWritingQueue.Enqueue("Application: " + DateTime.Now.ToString("MMddyyyy HH:mm:ss") + " Exception at archiving processed folder \r\n" + ex.Message); } } } } } } }
private static void ProcessedFolderBackup(string path) { try { string backupFolderName = ConfigurationManager.AppSettings["IndexFileBackupFolder"]; string backupFolderPrefix = ConfigurationManager.AppSettings["IndexFileFolderPrefix"]; string processedDirectory = path + "PROCESSED\\"; if (Directory.Exists(processedDirectory)) { foreach (var fileInfo in new DirectoryInfo(processedDirectory).GetFiles().OrderBy(x => x.LastWriteTime)) { string folderName = path + backupFolderName + "\\" + backupFolderPrefix + fileInfo.LastWriteTime.ToString("MM-yyyy") + "\\"; CustomFileHandling.CreateDirectoryIfDoesNotExist(folderName); CustomFileHandling.WaitForFile(fileInfo.FullName); File.Move(fileInfo.FullName, folderName + fileInfo.Name); } } } catch (Exception ex) { if (Logger.captureApplicationLogs) { Logger.Write("Application: " + DateTime.Now.ToString("MMddyyyy HH:mm:ss") + " Exception at ProcessedFolderBackup \r\n" + ex.Message); } } }
/// <summary> /// This event will fire if file name is renamed /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private static void OnRenamed(object sender, RenamedEventArgs e) { try { if (Path.GetExtension(e.FullPath).ToUpper() != ".TXT") { CustomFileHandling.WaitForFile(e.FullPath); string path = backupFolder + GetBackupFolderName(e.FullPath); File.Move(path + e.OldName, path + e.Name); } } catch (Exception ex) { if (Logger.captureApplicationLogs) { Logger.Write("Application: " + DateTime.Now.ToString("MMddyyyy HH:mm:ss") + " Exception at Renamed Event \r\n" + ex.Message); } } }
/// <summary> /// This function execute when create event happen in watcher folder /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private static void OnCreated(object sender, FileSystemEventArgs e) { try { if (Logger.captureEventsLogs) { Logger.Write("Event: " + e.FullPath + "\t IN \t" + System.DateTime.Now.ToString("MMddyyyy HH:mm:ss")); } if (Path.GetExtension(e.Name).ToUpper() != ".TXT") { CustomFileHandling.WaitForFile(e.FullPath); string pathToMove = backupFolder + GetBackupFolderName(e.FullPath); File.Copy(e.FullPath, pathToMove + e.Name, true); } } catch (Exception ex) { if (Logger.captureApplicationLogs) { Logger.Write("Application: " + DateTime.Now.ToString("MMddyyyy HH:mm:ss") + " Exception at Created Event \r\n" + ex.Message); } } }