SyncFolderInfo GetFolder(string path, bool withfile, List <SyncFolderInfo> folders) { var root = folders.Where(o => o.Name == path).First(); var namearr = root.Name.Split('\\'); SyncFolderInfo result = new SyncFolderInfo() { Name = namearr[namearr.Length - 1], //陣列最後一個是資料夾名稱 Path = path, CreateDate = root.CreateDate, UpdateDate = root.UpdateDate, Files = root.Files }; foreach (var folder in folders) { // 找出這個目錄下的子目錄 if (folder.Name != path && // 排除不是自己 folder.Name.Split('\\').Count() > 1 && // 排除不是第一層目錄 folder.Name.Replace(path + "\\", "").Split('\\').Count() == 1) //只要這個目錄下的第一層子目錄 { // 往下找 result.Folders.Add(GetFolder(folder.Name, withfile, folders)); } } return(result); }
SyncFolderInfo GetFolder(string path, bool withfile) { DirectoryInfo info = new DirectoryInfo(_basepath + path); SyncFolderInfo result = new SyncFolderInfo() { Name = info.Name, Path = path, CreateDate = info.CreationTime, UpdateDate = info.LastWriteTime }; string[] folders = Directory.GetDirectories(_basepath + path); foreach (string folder in folders) { result.Folders.Add(GetFolder(folder.Replace(_basepath, ""), withfile)); } if (withfile) { result.Files = GetFiles(path); } return(result); }
/// <summary> /// 建立資料夾、檔案 /// </summary> /// <param name="folder"></param> /// <param name="sourcelastrecord"></param> void CreateFolderAndFile(SyncFolderInfo folder, DateTime?sourcelastrecord, SyncResult result) { // 新增資料夾 if (_destination.CreateFolder(folder.Path)) { result.Folder++; // 若實際有增新資料夾,影響的資料夾+1 } foreach (var file in _source.GetFiles(folder.Path)) { // 檢查上次sync時間是否為null或 // 檔案是否在上次sync後有修改 if (!sourcelastrecord.HasValue || file.UpdateDate > sourcelastrecord.Value) { result.File++; // 影響的檔案+1 // 新增檔案 _destination.CreateFile( folder.Path, file.Name, _source.GetFile(file.Path) ); } } folder.Folders.ForEach(o => { CreateFolderAndFile(o, sourcelastrecord, result); }); }