public static void DownloadDirectoryFromFtp(string ftpUrl, string localFolderPath, string username, string password) { Queue> directoriesQueue = new Queue>(); directoriesQueue.Enqueue(new Tuple(ftpUrl, localFolderPath)); while (directoriesQueue.Count > 0) { var current = directoriesQueue.Dequeue(); string currentFtpUrl = current.Item1; string currentLocalPath = current.Item2; FtpWebRequest request = (FtpWebRequest)WebRequest.Create(currentFtpUrl); request.Method = WebRequestMethods.Ftp.ListDirectoryDetails; request.Credentials = new NetworkCredential(username, password); using (FtpWebResponse response = (FtpWebResponse)request.GetResponse()) using (StreamReader reader = new StreamReader(response.GetResponseStream())) { string line = reader.ReadLine(); while (!string.IsNullOrEmpty(line)) { string name = GetFileNameFromFtpLine(line); string permissions = GetPermissionsFromFtpLine(line); if (permissions.StartsWith("d")) { string subfolderFtpUrl = currentFtpUrl + "/" + name; string subfolderLocalPath = Path.Combine(currentLocalPath, name); if (!Directory.Exists(subfolderLocalPath)) { Directory.CreateDirectory(subfolderLocalPath); } directoriesQueue.Enqueue(new Tuple(subfolderFtpUrl, subfolderLocalPath)); } else { string fileUrl = currentFtpUrl + "/" + name; string localFilePath = Path.Combine(currentLocalPath, name); DownloadFileFromFtp(fileUrl, localFilePath, username, password); } line = reader.ReadLine(); } } } } public static void DownloadFileFromFtp(string fileUrl, string localFilePath, string username, string password) { FtpWebRequest request = (FtpWebRequest)WebRequest.Create(fileUrl); request.Method = WebRequestMethods.Ftp.DownloadFile; request.Credentials = new NetworkCredential(username, password); using (FtpWebResponse response = (FtpWebResponse)request.GetResponse()) using (Stream responseStream = response.GetResponseStream()) using (FileStream fileStream = new FileStream(localFilePath, FileMode.Create)) { responseStream.CopyTo(fileStream); } } private static string GetFileNameFromFtpLine(string ftpLine) { string[] tokens = ftpLine.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries); return tokens[tokens.Length - 1]; } private static string GetPermissionsFromFtpLine(string ftpLine) { string[] tokens = ftpLine.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries); return tokens[0]; }