// copy given file from source to target directory if appropriate public void SynchronizeFile(string file) { if (SourceAccessor.FileExists(file)) { if (CopyFile(file)) { #if DEBUG Console.WriteLine("copying from {1} to {2}: {0}", file, SourceAccessor, TargetAccessor); #endif using (var sourceStream = SourceAccessor.GetFileContents(file)) { DateTime writeTime = SourceAccessor.GetLastWriteTime(file); int attributes = SourceAccessor.GetFileAttributes(file); TargetAccessor.WriteFile(file, sourceStream, writeTime, attributes); } synchronizedFiles.Add(file); } } else if (DeleteAdditionalFiles && TargetAccessor.FileExists(file)) { #if DEBUG Console.WriteLine("deleting {0} from {1}", file, TargetAccessor); #endif TargetAccessor.DeleteFile(file); synchronizedFiles.Add(file); } }
// copy all files in given directory and iterate all subdirectories void CopyFilesFromDirectories(string dir) { foreach (string filename in SourceAccessor.GetFiles(dir)) { SynchronizeFile(filename); } foreach (string directory in SourceAccessor.GetDirectories(dir)) { CopyFilesFromDirectories(directory); } }
/* * Perform synchronization between the two directories. */ public void Synchronize(string directory = "") { if (SourceAccessor == null || TargetAccessor == null) { throw new InvalidOperationException("Cannot decide source or target directory"); } if (CopyFile == null) { throw new InvalidOperationException("Cannot decide what files to copy"); } #if DEBUG Console.WriteLine("synchronizing {2} from {0} to {1}", SourceAccessor, TargetAccessor, directory); #endif // remove files that are in the target, but not in the source folder if (DeleteAdditionalFiles) { DeleteFilesFromTarget(directory); } // copy files from the source to the target folder if (SourceAccessor.DirectoryExists(directory)) { CopyFilesFromDirectories(directory); } }