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
- string ftpDownloadResult = string.Empty;
- try
- {
- WebRequest request = WebRequest.Create(ConfigurationManager.AppSettings["FTPPath"]);
- request.Method = WebRequestMethods.Ftp.ListDirectory;
- request.Credentials = newNetworkCredential(ConfigurationManager.AppSettings["FTPUserName"],
- ConfigurationManager.AppSettings["FTPPassword"]);
- using(var response = (FtpWebResponse) request.GetResponse())
- {
- StreamReader streamReader = newStreamReader(response.GetResponseStream());
- List < string > directories = newList < string > ();
- string line = streamReader.ReadLine();
- while (!string.IsNullOrEmpty(line))
- {
- directories.Add(line);
- line = streamReader.ReadLine();
- }
- streamReader.Close();
- using(WebClient ftpClient = newWebClient())
- {
- ftpClient.Credentials =
- new System.Net.NetworkCredential(
- ConfigurationManager.AppSettings["FTPUserName"],
- ConfigurationManager.AppSettings["FTPPassword"]);
- for (int i = 0; i <= directories.Count– 1; i++)
- {
- if (directories[i].Contains(“.”))
- {
- string path = ConfigurationManager.AppSettings["FTPPath"] + directories[i].ToString();
- string trnsfrpth = ConfigurationManager.AppSettings["SaveFilePath"] + directories[i].ToString();
- ftpClient.DownloadFile(path, trnsfrpth);
- }
- }
- }
- }
Step 2
- Add the code, mentioned below to app.config
- <appSettings> < add key="FTPPath" value="ftp://test.com/MyFolder"></add> < add key="FTPUserName" value="USERNAME" /> <add key="FTPPassword" value="PASSWORD" />
- <addkeyaddkey="SaveFilePath"value="C:\HRData\" />
- < /appSettings>
- <system.net> < defaultProxy useDefaultCredentials="true"> <proxy autoDetect="True"/> < /defaultProxy> < /system.net>

Irving GutiérrezPosted Apr 27, 2022, 3:23 PM
Very well, thanks!