title: Retrieve Specific Files from Directory Using FTP
body: <p>I'm creating a C# application that needs to FTP to a directory to retrieve a file list. The following code works just fine. However, the folder that I'm FTPing to contains around 92,000 files. This code will not work in the way that I want it to for a file list of that size.</p>
  
  <p>I'm looking only for files that begin with the string "c-". After doing some research, I'm not even sure how to begin trying to solve this issue. Is there any way I can modify this existing code for it to retrieve only those files?</p>
  
  <pre><code>public string[] getFileList() {
      string[] downloadFiles;
      StringBuilder result = new StringBuilder();
      FtpWebRequest reqFTP;
  
      try {
          reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(ftpHost));
          reqFTP.UseBinary = true;
          reqFTP.Credentials = new NetworkCredential(ftpUser, ftpPass);
          reqFTP.Method = WebRequestMethods.Ftp.ListDirectory;
          WebResponse response = reqFTP.GetResponse();
          StreamReader reader = new StreamReader(response.GetResponseStream());
  
          string line = reader.ReadLine();
          while (line != null) {
              result.Append(line);
              result.Append("\n");
              line = reader.ReadLine();
          }
          // to remove the trailing '\n'
          result.Remove(result.ToString().LastIndexOf('\n'), 1);
          reader.Close();
          response.Close();
          return result.ToString().Split('\n');
      }
      catch (Exception ex) {
          System.Windows.Forms.MessageBox.Show(ex.Message);
          downloadFiles = null;
          return downloadFiles;
      }
  }
  </code></pre>