Introduction

  • Here, I will explain how to read multiple files from the folder, using FTP location. Please follow the steps mentioned below to fetch.
  • Follow the steps mentioned below to achieve our requirement.
Step 1
  • Place the code in Class file
  1. string ftpDownloadResult = string.Empty;
  2. try
  3. {
  4. WebRequest request = WebRequest.Create(ConfigurationManager.AppSettings["FTPPath"]);
  5. request.Method = WebRequestMethods.Ftp.ListDirectory;
  6. request.Credentials = newNetworkCredential(ConfigurationManager.AppSettings["FTPUserName"],
  7. ConfigurationManager.AppSettings["FTPPassword"]);
  8. using(var response = (FtpWebResponse) request.GetResponse())
  9. {
  10. StreamReader streamReader = newStreamReader(response.GetResponseStream());
  11. List < string > directories = newList < string > ();
  12. string line = streamReader.ReadLine();
  13. while (!string.IsNullOrEmpty(line))
  14. {
  15. directories.Add(line);
  16. line = streamReader.ReadLine();
  17. }
  18. streamReader.Close();
  19. using(WebClient ftpClient = newWebClient())
  20. {
  21. ftpClient.Credentials =
  22. new System.Net.NetworkCredential(
  23. ConfigurationManager.AppSettings["FTPUserName"],
  24. ConfigurationManager.AppSettings["FTPPassword"]);
  25. for (int i = 0; i <= directories.Count– 1; i++)
  26. {
  27. if (directories[i].Contains(“.”))
  28. {
  29. string path = ConfigurationManager.AppSettings["FTPPath"] + directories[i].ToString();
  30. string trnsfrpth = ConfigurationManager.AppSettings["SaveFilePath"] + directories[i].ToString();
  31. ftpClient.DownloadFile(path, trnsfrpth);
  32. }
  33. }
  34. }
  35. }
Step 2
  • Add the code, mentioned below to app.config
  1. <appSettings> < add key="FTPPath" value="ftp://test.com/MyFolder"></add> < add key="FTPUserName" value="USERNAME" /> <add key="FTPPassword" value="PASSWORD" />
  2. <addkeyaddkey="SaveFilePath"value="C:\HRData\" />
  3. < /appSettings>
  4. <system.net> < defaultProxy useDefaultCredentials="true"> <proxy autoDetect="True"/> < /defaultProxy> < /system.net>