title: C# Download all files and subdirectories through FTP
body: <p><strong>General Info</strong><br />
  I'm still in the process of learning C#. To help myself out, I'm trying to create a program that will automatically synchronise all of my local projects with a folder on my FTP server. This so that whether I'm at school or at home, I always have the same projects available to me.</p>
  
  <p>I know there are programs like Dropbox that already do this for me, but I figured creating something like that myself will teach me a lot along the way.</p>
  
  <p><strong>The problem</strong><br />
  My first step towards my goal was to just download all files, subdirectories and subfiles from my FTP server. I've managed to download all files from a directory with the code below. However, my code only lists the folder names and the files in the main directory. Subfolders and subfiles are never returned and never downloaded. Aside from that, the server returns a 550 error because I'm trying to download the folders as if they are files. I've been on this for 4+ hours now, but I just can't find anything on how to fix these problems and make it work. Therefor I'm hoping you guys will help me out :)</p>
  
  <p><strong>Code</strong></p>
  
  <pre><code>public string[] GetFileList()
  {
      string[] downloadFiles;
      StringBuilder result = new StringBuilder();
      WebResponse response = null;
      StreamReader reader = null;
  
      try
      {
          FtpWebRequest request = (FtpWebRequest)WebRequest.Create(url);
          request.UseBinary = true;
          request.Method = WebRequestMethods.Ftp.ListDirectory;
          request.Credentials = new NetworkCredential(ftpUserName, ftpPassWord);
          request.KeepAlive = false;
          request.UsePassive = false;
          response = request.GetResponse();
          reader = new StreamReader(response.GetResponseStream());
          string line = reader.ReadLine();
          while (line != null)
          {
              result.Append(line);
              result.Append("\n");
              line = reader.ReadLine();
          }
          result.Remove(result.ToString().LastIndexOf('\n'), 1);
          return result.ToString().Split('\n');
      }
      catch (Exception ex)
      {
          if (reader != null)
          {
              reader.Close();
          }
          if (response != null)
          {
              response.Close();
          }
          downloadFiles = null;
          return downloadFiles;
      }
  }
  
  private void Download(string file)
  {
      try
      {
          string uri = url + "/" + file;
          Uri serverUri = new Uri(uri);
          if (serverUri.Scheme != Uri.UriSchemeFtp)
          {
              return;
          }
          FtpWebRequest request = (FtpWebRequest)WebRequest.Create(url + "/" + file);
          request.UseBinary = true;
          request.Method = WebRequestMethods.Ftp.DownloadFile;
          request.Credentials = new NetworkCredential(ftpUserName, ftpPassWord);
          request.KeepAlive = false;
          request.UsePassive = false;
          FtpWebResponse response = (FtpWebResponse)request.GetResponse();
          Stream responseStream = response.GetResponseStream();
          FileStream writeStream = new FileStream(localDestnDir + "\\" + file, FileMode.Create);                
          int Length = 2048;
          Byte[] buffer = new Byte[Length];
          int bytesRead = responseStream.Read(buffer, 0, Length);
          while (bytesRead &gt; 0)
          {
              writeStream.Write(buffer, 0, bytesRead);
              bytesRead = responseStream.Read(buffer, 0, Length);
          }
          writeStream.Close();
          response.Close();
      }
      catch (WebException wEx)
      {
          MessageBox.Show(wEx.Message, "Download Error");
      }
      catch (Exception ex)
      {
          MessageBox.Show(ex.Message, "Download Error");
      }
  }
  </code></pre>