Download File From FTP With Sub-Directories Using C#

When I got the task to download the files from FTP with subdirectories, I searched many articles and many posts for the sample code but I get partially. i.e only download the file by giving end path, as shown below (FTPaddress/Directory/Sub-Dir1/Sub-Dir2/Sub-Dir3/Filename.zip) but I want to download the file by giving only this (FTPaddress/Directory). After the hours of work, I got the output. I want to share the code. I think, it will help the people while they struggle in this part.

Let's enter into the code.
  1. public void DownloadFile() {  
  2.     string dirpath = txtlocalpath.Text;  
  3.     try {  
  4.         FtpWebRequest ftpRequest = (FtpWebRequest) WebRequest.Create("ftp://xxx.xxx.xx.xx/"); // FTP Address  
  5.         ftpRequest.Credentials = new NetworkCredential(txtusername.Text, txtpassword.Text); // Credentials  
  6.         ftpRequest.Method = WebRequestMethods.Ftp.ListDirectory;  
  7.         FtpWebResponse response = (FtpWebResponse) ftpRequest.GetResponse();  
  8.         StreamReader streamReader = new StreamReader(response.GetResponseStream());  
  9.         List < string > directories = new List < string > (); // create list to store directories.   
  10.         string line = streamReader.ReadLine();  
  11.         while (!string.IsNullOrEmpty(line)) {  
  12.             directories.Add(line); // Add Each Directory to the List.  
  13.             line = streamReader.ReadLine();  
  14.         }  
  15.         using(WebClient ftpClient = new WebClient()) {  
  16.             ftpClient.Credentials = new NetworkCredential(txtusername.Text, txtpassword.Text);  
  17.             string[] filename = txtftpaddress.Text.Split('/');  
  18.             Filename = filename[filename.Length - 1];  
  19.             for (int k = 0; k <= filename.Length - 1; k++) {  
  20.                 dirpath = dirpath + "\\" + filename[k];  
  21.                 createdir(dirpath);  
  22.             }  
  23.             try {  
  24.                 for (int i = 0; i <= directories.Count - 1; i++) {  
  25.                     if (directories[i].Contains(".")) {  
  26.                         string path = "ftp://xxx.xxx.xx.xx//" + txtftpaddress.Text + "/" + directories[i].ToString();  
  27.                         ftpClient.DownloadFile(path, dirpath.ToString() + "\\" + directories[i].ToString());  
  28.                     } else {  
  29.                         string path = "ftp://xxx.xxx.xx.xx//" + txtftpaddress.Text + "/" + directories[i].ToString();  
  30.                         string subdirpath = dirpath + "\\" + directories[i].ToString();  
  31.                         createdir(subdirpath);  
  32.                         string[] subdirectory = Return(path, txtusername.Text, txtpassword.Text);  
  33.                         for (int j = 0; j <= subdirectory.Length - 1; j++) {  
  34.                             string subpath = directories[i] + "/" + subdirectory[j];  
  35.                             directories.Add(subpath); // Add the Sub-directories with the path to directories.  
  36.                         }  
  37.                     }  
  38.                 }  
  39.             } catch (WebException e) {  
  40.                 String status = ((FtpWebResponse) e.Response).StatusDescription;  
  41.                 MessageBox.Show(status.ToString());  
  42.             }  
  43.         }  
  44.         streamReader.Close();  
  45.     } catch (WebException l) {  
  46.         String status = ((FtpWebResponse) l.Response).StatusDescription;  
  47.         MessageBox.Show(status.ToString());  
  48.     }  
  49. }  
  50. // Here i get the list of Sub-directories and the files.   
  51. public string[] Return(string filepath, string username, string password) {  
  52.     List < string > directories = new List < string > ();  
  53.     try {  
  54.         FtpWebRequest ftpRequest = (FtpWebRequest) WebRequest.Create(filepath);  
  55.         ftpRequest.Credentials = new NetworkCredential(username, password);  
  56.         ftpRequest.Method = WebRequestMethods.Ftp.ListDirectory;  
  57.         FtpWebResponse response = (FtpWebResponse) ftpRequest.GetResponse();  
  58.         StreamReader streamReader = new StreamReader(response.GetResponseStream());  
  59.         string line = streamReader.ReadLine();  
  60.         while (!string.IsNullOrEmpty(line)) {  
  61.             directories.Add(line);  
  62.             line = streamReader.ReadLine();  
  63.         }  
  64.     } catch (WebException e) {  
  65.         String status = ((FtpWebResponse) e.Response).StatusDescription;  
  66.         System.Windows.Forms.MessageBox.Show(status.ToString());  
  67.     }  
  68.     return directories.ToArray();  
  69. }  
  70. // In this part i create the sub-directories.   
  71. public void createdir(string path) {  
  72.     if (!Directory.Exists(path)) {  
  73.         Directory.CreateDirectory(path);  
  74.     }  
  75. }  
Ebook Download
View all
Learn
View all