Connecting Tech Pros Worldwide Forums | Help | Site Map

Downloading all files in directory using FTP

Newbie
 
Join Date: May 2009
Posts: 1
#1: May 27 '09
Hello,

I am trying to download not one, but all files within a remote directory using FTP and C# and then save them to a local folder on my hard drive. How can I do this?

thanks

tlhintoq's Avatar
Moderator
 
Join Date: Mar 2008
Location: Arizona, USA
Posts: 1,777
#2: May 27 '09

re: Downloading all files in directory using FTP


Get a list of all the files.
Add them to a list of files to be downloaded.
Loop through your list downloading one at a time.
When it is done, take it off you list of 'to be downloaded'
Next file on the list.

It's pretty much like your downloading of a single file, but with a 'foreach loop' wrapped around it.

Expand|Select|Wrap|Line Numbers
  1. List<string> FilesToDownload = new LIst<string>();
  2. FilesToDownload = GetTheFtpDirectoryContentsMethod(URLasStringParameter);//Returns a List
  3. foreach (string YogiBear in FilesToDownload)
  4. {
  5.      YourSingleFileDownloadMethod(YogiBear, LocalDestinationPath);
  6. }
  7.  

You probably want to do a few things like get the sizes of all the files to be downloaded and make sure there is sufficient room on the local directory. Do some checks that you can reach the source URL, that you have write permissions on the local destination and so on. Basic error handling stuff. But if you start with the stripped-down logic you can bolt on more and more armor until it's bullet proof.
Reply

Tags
c-sharp, download, ftp