public static void SyncDirectory(string sourcePath, string targetPath, OnSyncSuccessAction action = null, string[] ignoreFileName = null) { ForEachFile(sourcePath, fullName => { var fileName = Path.GetFileName(fullName); if (ignoreFileName != null && ignoreFileName.Contains(fileName)) { return; } string _sourcePath = fullName; string _targetPath = fullName.Replace(sourcePath, targetPath); SyncFile(_sourcePath, _targetPath, action); }); }
public static void SyncFile(string sourcePath, string targetPath, OnSyncSuccessAction action = null) { if (!File.Exists(sourcePath)) { throw new FileNotFoundException(sourcePath); } if (!File.Exists(targetPath) || (File.Exists(targetPath) && File.GetLastWriteTimeUtc(sourcePath) != File.GetLastWriteTimeUtc(targetPath))) { var dirPath = Path.GetDirectoryName(targetPath); if (!Directory.Exists(dirPath)) { Directory.CreateDirectory(dirPath); } File.Copy(sourcePath, targetPath, true); action?.Invoke(sourcePath); } }