using System; using System.IO; namespace MergeHub.Services { /// /// 폴더 비교에서 실제로 파일을 옮기고 지우는 부분. /// /// ViewModel 이 아니라 여기 두는 이유 - 화면 없이 검사할 수 있어야 한다. /// 되돌릴 수 없는 일이라 "정말 그렇게 도는지"를 눈이 아니라 테스트로 확인해야 한다. /// public static class FileOps { /// /// 폴더 안에 든 파일 수(하위 폴더 포함). /// 확인 창에 "몇 개가 덮이는지"를 적어 주려고 쓴다 - "폴더 하나"라고만 물으면 /// 몇 개가 사라지는지 모르는 채 확인을 누르게 된다. /// public static int CountFiles(string folder) { try { return Directory.GetFiles(folder, "*", SearchOption.AllDirectories).Length; } catch (IOException) { return 0; // 셀 수 없으면 0 으로 두고 진행 여부는 사람이 판단한다 } catch (System.UnauthorizedAccessException) { return 0; } } /// /// 폴더를 통째로 복사한다. 대상에 이미 있는 파일은 덮어쓰고, 대상에만 있는 파일은 그대로 둔다. /// (지우면서 맞추는 "동기화"가 아니다 - 지우는 것은 삭제 버튼이 따로 맡는다) /// /// 있는 파일을 덮어쓰기 직전에 부른다(11차 F-9 백업). 던지면 그 파일부터 멈춘다. public static void CopyDirectory(string from, string to, Action? beforeOverwrite = null) { Directory.CreateDirectory(to); foreach (var file in Directory.GetFiles(from)) { var target = Path.Combine(to, Path.GetFileName(file)); if (beforeOverwrite is not null && File.Exists(target)) beforeOverwrite(target); File.Copy(file, target, overwrite: true); } foreach (var child in Directory.GetDirectories(from)) CopyDirectory(child, Path.Combine(to, Path.GetFileName(child)), beforeOverwrite); } } }