Esempio n. 1
0
        public static void DownloadFtpDirectory(NetworkCredential creds, string host, string remoteDir, string destinationDir, ShouldDownloadFile sdlf)
        {
            Directory.CreateDirectory(destinationDir);

            using (FtpClient ftp = new FtpClient {
                Credentials = creds, Host = host
            })
            {
                ftp.Connect();

                var listing = ftp.GetListing(remoteDir);

                foreach (var item in listing)
                {
                    if (item.Type != FtpFileSystemObjectType.File)
                    {
                        continue;
                    }

                    try
                    {
                        TryDownloadFile(ftp, item, remoteDir, destinationDir, sdlf);
                    }
                    catch (IOException ex)
                    {
                        throw new Exception("Unable to download file.", ex);
                    }
                }
            }
        }
Esempio n. 2
0
        private static void TryDownloadFile(FtpClient ftpClient, FtpListItem item, string remoteDir, string destinationDir, ShouldDownloadFile sdlf)
        {
            bool shouldDownload = sdlf(item.Name);

            if (!shouldDownload)
            {
                return;
            }

            string fileFullPath = destinationDir + "/" + item.Name;

            using (var stream = ftpClient.OpenRead(remoteDir + "/" + item.Name, FtpDataType.Binary))
            {
                if (stream.Length != 0)
                {
                    // Create a FileStream object to write a stream to a file
                    using (FileStream fileStream = File.Create(fileFullPath, (int)stream.Length))
                    {
                        stream.CopyTo(fileStream);
                    }
                }
            }
        }