/// <summary> /// FTP 서버의 <paramref name="directory"/>의 하위 디렉토리 및 파일명을 열거합니다. /// </summary> /// <param name="ftpClient"></param> /// <param name="directory"></param> /// <param name="recursive">재귀 호출 여부</param> /// <returns></returns> public static Task <IList <string> > ListDirectoryTask(this FtpClient ftpClient, string directory, bool recursive) { if (IsDebugEnabled) { log.Debug("List all entry in the specified directory. directory=[{0}], recursive=[{1}]", directory, recursive); } var paths = new List <string>(); directory = FtpClient.AdjustDir(directory); paths.Add(directory); if (!directory.EndsWith(FtpClient.FTP_PATH_DELIMITER)) { directory += FtpClient.FTP_PATH_DELIMITER; } var task = ftpClient.ListDirectoryTask(directory); if (recursive) { task.ContinueWith(dt => dt.Result .Select(subDir => FtpClient.PathCombine(directory, subDir)) .RunEach(subPath => paths.AddRange(ftpClient.ListDirectoryTask(subPath, true).Result)), TaskContinuationOptions.ExecuteSynchronously); } else { paths.AddRange(task.Result); } return(Task.Factory.FromResult <IList <string> >(paths)); }
/// <summary> /// FTP 서버의 지정한 디렉토리를 삭제합니다. /// </summary> /// <param name="ftpClient"></param> /// <param name="remoteDir"></param> /// <param name="recursive"></param> /// <returns></returns> public static Task <bool> DeleteDirectoryTask(this FtpClient ftpClient, string remoteDir, bool recursive) { remoteDir.ShouldNotBeWhiteSpace("remoteDir"); if (IsDebugEnabled) { log.Debug("원격 디렉토리를 비동기 방식으로 삭제합니다... remoteDir=[{0}], recursive=[{1}]", remoteDir, recursive); } if (ftpClient.DirectoryExistsTask(remoteDir).Result == false) { return(Task.Factory.FromResult(true)); } if (recursive) { // 디렉토리에 파일이 있을 경우에는 삭제가 안됩니다!!! // var subDirs = ftpClient.ListDirectoryTask(remoteDir).Result; if (subDirs.Count > 0) { var tasks = new List <Task>(); foreach (var dir in subDirs) { var task = ftpClient.DeleteDirectoryTask(remoteDir + FtpClient.FTP_PATH_DELIMITER + dir, true); tasks.Add(task); } Task.WaitAll(tasks.ToArray()); } } return(ftpClient.DeleteDirectoryTask(remoteDir)); }
/// <summary> /// FTP 서버에 <paramref name="directory"/> 가 존재하는지 확인합니다. /// </summary> /// <param name="ftpClient"></param> /// <param name="directory"></param> /// <returns></returns> public static Task <bool> DirectoryExistsTask(this FtpClient ftpClient, string directory) { if (IsDebugEnabled) { log.Debug("원격 디렉토리가 존재하는지 확인하는 비동기 작업을 directory=[{0}]", directory); } directory = FtpClient.AdjustDir(directory); if (directory == FtpClient.FTP_PATH_DELIMITER) { return(Task.Factory.FromResult(true)); } var tcs = new TaskCompletionSource <bool>(); var isSuccess = false; try { // 디렉토리에 대해 리스트를 조회해본다. // 조회가 된다면, 서브 디렉토리가 없더라도, 550 예외가 발생하지 않으므로, 디렉토리가 존재한다. // 디렉토리가 없다면 550 예외가 발생하므로, false 이다. // ftpClient.ListDirectoryTask(directory).Wait(); if (IsDebugEnabled) { log.Debug("원격 디렉토리가 존재합니다. directory=[{0}]", directory); } isSuccess = true; } catch (AggregateException age) { age.Handle(error => true); } tcs.TrySetResult(isSuccess); return(tcs.Task); }