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); } } } } } } }
/// <summary> /// Write logs using dequeue service /// </summary> public static void DequeueService() { string logFile = CustomFileHandling.GetOrCreateLogFile(System.Configuration.ConfigurationManager.AppSettings["LogsFolder"].ToString()); try { if (AddtoWritingQueue.Count > 0) { foreach (object textLine in AddtoWritingQueue) { if (logFile != string.Empty) { using (StreamWriter sw = new StreamWriter(logFile, true)) { sw.WriteLine(AddtoWritingQueue.Dequeue().ToString()); } } } } } catch { } finally { } }
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); } } }
/// <summary> /// This function executes when delete event happen in your watcher folder /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private static void OnDeleted(object sender, FileSystemEventArgs e) { try { if (Logger.captureEventsLogs) { Logger.Write("Event: " + e.FullPath + "\t OUT \t" + System.DateTime.Now.ToString("MMddyyyy HH:mm:ss")); } if (Path.GetExtension(e.FullPath).ToUpper() != ".TXT") { string pathToMove = DeletedCopyFolder + GetBackupFolderName(e.FullPath, true); CustomFileHandling.CreateDirectoryIfDoesNotExist(pathToMove); File.Move(backupFolder + GetBackupFolderName(e.FullPath) + e.Name, pathToMove + AppendTimeStamp(e.Name)); } } catch (Exception ex) { if (Logger.captureApplicationLogs) { Logger.Write("Application: " + DateTime.Now.ToString("MMddyyyy HH:mm:ss") + " Exception at Deleted Event \r\n" + ex.Message); } } }
/// <summary> /// Create direcotries if not exist. Prepares structure as per the configuration file. /// </summary> private static void PrepareFolderStucture(string watchPath) { //Debugger.Launch(); CustomFileHandling.CreateDirectoryIfDoesNotExist(DeletedCopyFolder + GetBackupFolderName(watchPath, true)); }
static private void SendNotification() { try { string[] watchPathArray = System.Configuration.ConfigurationManager.AppSettings["NotificationPaths"].ToString().Split('|'); double threshold = Convert.ToDouble(ConfigurationManager.AppSettings["WatchThresholdTimeInMin"]); string message = @"<html> <head> </head> <body><h2>This is an auto-generated email</h2><br /><br />Hi Team,<br /><br />"; bool dataFound = false; foreach (string watchPath in watchPathArray) { int counter = 0; foreach (var fileInfo in new DirectoryInfo(watchPath).GetFiles().OrderBy(x => x.LastWriteTime)) { DateTime latestTime = getTime(fileInfo.LastWriteTime, fileInfo.LastAccessTime, fileInfo.CreationTime); double totalExistsMinute = (System.DateTime.Now - latestTime).TotalMinutes; if (fileInfo.Name != "Thumbs.db") { if (totalExistsMinute > threshold) { counter++; dataFound = true; if (watchPath.ToUpper().Contains("ERROR_FILES")) { string fileToMove = watchPath + "\\" + DateTime.Now.ToString("MMddyyyy"); CustomFileHandling.CreateDirectoryIfDoesNotExist(fileToMove); File.Move(fileInfo.FullName, fileToMove + "\\" + fileInfo.Name); } } } } if (counter > 0) { if (counter > 1) { message += counter + " documents are sitting for more than " + threshold + " minutes on the below path<br /><b>" + watchPath + "</b><br /><br />"; } else { message += "1 document is sitting for more than " + threshold + " minutes on the below path<br /><b>" + watchPath + "</b><br /><br />"; } } } if (dataFound) { message += "Please take immediate action.<br /><br />-FolderWatcher Notification</body></html>"; if (Convert.ToBoolean(ConfigurationManager.AppSettings["WriteNotification"].Trim()) == true) { Logger.AddtoWritingQueue.Enqueue("INFO: Email notification at " + System.DateTime.Now.ToString("MMddyyyy HH:mm:ss")); Thread.Sleep(100); } if (Convert.ToBoolean(ConfigurationManager.AppSettings["EmailNotification"].Trim()) == true) { Mailer.SendMail(message); } } } catch (Exception ex) { if (Logger.captureApplicationLogs) { Logger.AddtoWritingQueue.Enqueue("Error : occured in Notification service -" + ex.Message); Thread.Sleep(100); } } }