public AutoCopy(TransferLocations transferLocations) { //default copy interval CopyInterval = 5 * 60 * 1000; _arbyterCopyInstance = ArbyterCoreFactory.Instance.GetArbyterCopy(); _transferLocations = transferLocations; _timer = new Timer(); _timer.AutoReset = true; _timer.Elapsed += new ElapsedEventHandler(TimerElapsedEvent); }
public AutoCopy(TransferLocations transferLocations) { //default copy interval CopyInterval = 5 * 60 * 1000; _arbyterCopyInstance = ArbyterCoreFactory.Instance.GetArbyterCopy(); _transferLocations = transferLocations; _timer = new Timer(); _timer.AutoReset = true; _timer.Elapsed += new ElapsedEventHandler(TimerElapsedEvent); }
//checks if file should be copied private bool ShouldCopyFile(string filename, TransferLocations transferLocations) { FileInfo sourceFile = new FileInfo(transferLocations.SourcePath + "\\" + filename); FileInfo destinationFile = new FileInfo(transferLocations.DestinationPath + "\\" + filename); if (!destinationFile.Exists || destinationFile.LastWriteTime < sourceFile.LastWriteTime) { return(true); } return(false); }
//checks if file should be deleted private bool ShouldDeleteFile(string filename, TransferLocations transferLocations) { //do not delete the log if (filename.Equals("Arbyter_Log.txt")) { return(false); } FileInfo sourceFile = new FileInfo(transferLocations.SourcePath + "\\" + filename); FileInfo destinationFile = new FileInfo(transferLocations.DestinationPath + "\\" + filename); if (!sourceFile.Exists && destinationFile.Exists) { return(true); } return(false); }
//copies necessary files from source folder hierarchy private void CopyModifiedContent(TransferLocations transferLocations) { var sourceDirectory = new DirectoryInfo(transferLocations.SourcePath); var childDirectories = sourceDirectory.GetDirectories(); foreach (var childDirectory in childDirectories) { CopyModifiedContent(new TransferLocations( childDirectory.FullName, transferLocations.DestinationPath + "\\" + childDirectory.Name, transferLocations.Logging)); } var destinationDirectory = new DirectoryInfo(transferLocations.DestinationPath); if (!destinationDirectory.Exists) destinationDirectory.Create(); CopyModifiedFiles(transferLocations); }
//deletes the necessay files from current folder private void DeleteExtraFiles(TransferLocations transferLocations) { var destinationDirectory = new DirectoryInfo(transferLocations.DestinationPath); foreach (var fileInfo in destinationDirectory.GetFiles()) { if (ShouldDeleteFile(fileInfo.Name, transferLocations)) { if (transferLocations.Logging) LogFile.WriteLine("[{0}] Deleting {1}\\{2}", DateTime.Now, transferLocations.DestinationPath, fileInfo.Name); _deletedFileCount++; //copy overwriting existing file, if necessary fileInfo.Delete(); } } }
//copies the necessay files from current folder private void CopyModifiedFiles(TransferLocations transferLocations) { var sourceDirectory = new DirectoryInfo(transferLocations.SourcePath); foreach (var fileInfo in sourceDirectory.GetFiles()) { if (ShouldCopyFile(fileInfo.Name, transferLocations)) { if (transferLocations.Logging) LogFile.WriteLine("[{0}] Copying {1}\\{2} to {3}\\{4}", DateTime.Now, transferLocations.SourcePath, fileInfo.Name, transferLocations.DestinationPath, fileInfo.Name); _copiedFileCount++; //copy overwriting existing file, if necessary fileInfo.CopyTo(transferLocations.DestinationPath + "\\" + fileInfo.Name, true); } } }
//copies necessary files from source folder hierarchy private void CopyModifiedContent(TransferLocations transferLocations) { var sourceDirectory = new DirectoryInfo(transferLocations.SourcePath); var childDirectories = sourceDirectory.GetDirectories(); foreach (var childDirectory in childDirectories) { CopyModifiedContent(new TransferLocations( childDirectory.FullName, transferLocations.DestinationPath + "\\" + childDirectory.Name, transferLocations.Logging)); } var destinationDirectory = new DirectoryInfo(transferLocations.DestinationPath); if (!destinationDirectory.Exists) { destinationDirectory.Create(); } CopyModifiedFiles(transferLocations); }
//deletes the necessay files from current folder private void DeleteExtraFiles(TransferLocations transferLocations) { var destinationDirectory = new DirectoryInfo(transferLocations.DestinationPath); foreach (var fileInfo in destinationDirectory.GetFiles()) { if (ShouldDeleteFile(fileInfo.Name, transferLocations)) { if (transferLocations.Logging) { LogFile.WriteLine("[{0}] Deleting {1}\\{2}", DateTime.Now, transferLocations.DestinationPath, fileInfo.Name); } _deletedFileCount++; //copy overwriting existing file, if necessary fileInfo.Delete(); } } }
//copies the necessay files from current folder private void CopyModifiedFiles(TransferLocations transferLocations) { var sourceDirectory = new DirectoryInfo(transferLocations.SourcePath); foreach (var fileInfo in sourceDirectory.GetFiles()) { if (ShouldCopyFile(fileInfo.Name, transferLocations)) { if (transferLocations.Logging) { LogFile.WriteLine("[{0}] Copying {1}\\{2} to {3}\\{4}", DateTime.Now, transferLocations.SourcePath, fileInfo.Name, transferLocations.DestinationPath, fileInfo.Name); } _copiedFileCount++; //copy overwriting existing file, if necessary fileInfo.CopyTo(transferLocations.DestinationPath + "\\" + fileInfo.Name, true); } } }
//starts the run public ActionReport RunCopy(TransferLocations transferLocations) { try { _copiedFileCount = 0; if (transferLocations.Logging) { LogFile = new StreamWriter(transferLocations.DestinationPath + "\\Arbyter_Log.txt", true); LogFile.WriteLine("[{0}] Arbyter Copy Run Started", DateTime.Now); } CopyModifiedContent(transferLocations); if (transferLocations.Logging) { LogFile.WriteLine("{0} file(s) copied", _copiedFileCount); LogFile.Close(); } return(new ActionReport(_copiedFileCount, ActionResult.Success, null)); } catch (Exception exception) { return(new ActionReport(null, ActionResult.Failure, exception)); } }
//copies necessary files from source folder hierarchy private void DeleteExtraContent(TransferLocations transferLocations) { var destinationDirectory = new DirectoryInfo(transferLocations.DestinationPath); var childDirectories = destinationDirectory.GetDirectories(); foreach (var childDirectory in childDirectories) { var sourceChildDir = new DirectoryInfo(transferLocations.SourcePath + "\\" + childDirectory.Name); //if the chiled does not exist in the source then delete it immediately if (!sourceChildDir.Exists) { _deletedFileCount += childDirectory.GetFiles("*", SearchOption.AllDirectories).Count(); childDirectory.Delete(true); continue; } DeleteExtraContent(new TransferLocations( sourceChildDir.FullName, childDirectory.FullName, transferLocations.Logging)); } DeleteExtraFiles(transferLocations); }
//copies necessary files from source folder hierarchy private void DeleteExtraContent(TransferLocations transferLocations) { var destinationDirectory = new DirectoryInfo(transferLocations.DestinationPath); var childDirectories = destinationDirectory.GetDirectories(); foreach (var childDirectory in childDirectories) { var sourceChildDir = new DirectoryInfo(transferLocations.SourcePath + "\\" + childDirectory.Name); //if the chiled does not exist in the source then delete it immediately if (!sourceChildDir.Exists) { _deletedFileCount += childDirectory.GetFiles("*", SearchOption.AllDirectories).Count(); childDirectory.Delete(true); continue; } DeleteExtraContent(new TransferLocations( sourceChildDir.FullName, childDirectory.FullName, transferLocations.Logging)); } DeleteExtraFiles(transferLocations); }
//starts the run public ActionReport RunClean(TransferLocations transferLocations) { try { _deletedFileCount = 0; if (transferLocations.Logging) { LogFile = new StreamWriter(transferLocations.DestinationPath + "\\Arbyter_Log.txt", true); LogFile.WriteLine("[{0}] Arbyter Clean Run Started", DateTime.Now); } DeleteExtraContent(transferLocations); if (transferLocations.Logging) { LogFile.WriteLine("{0} file(s) deleted", _deletedFileCount); LogFile.Close(); } return new ActionReport(_deletedFileCount, ActionResult.Success, null); } catch (Exception exception) { return new ActionReport(null, ActionResult.Failure, exception); } }
//checks if file should be deleted private bool ShouldDeleteFile(string filename, TransferLocations transferLocations) { //do not delete the log if (filename.Equals("Arbyter_Log.txt")) return false; FileInfo sourceFile = new FileInfo(transferLocations.SourcePath + "\\" + filename); FileInfo destinationFile = new FileInfo(transferLocations.DestinationPath + "\\" + filename); if (!sourceFile.Exists && destinationFile.Exists) return true; return false; }
//checks if file should be copied private bool ShouldCopyFile(string filename, TransferLocations transferLocations) { FileInfo sourceFile = new FileInfo(transferLocations.SourcePath + "\\" + filename); FileInfo destinationFile = new FileInfo(transferLocations.DestinationPath + "\\" + filename); if (!destinationFile.Exists || destinationFile.LastWriteTime < sourceFile.LastWriteTime) return true; return false; }