Read Multiple Files from folder and Download using FTP location in Console Application using C#

Read Multiple Files from folder and Download using FTP location in Console Application using C#

Today I’m going to tell you to read multiple files from folder using FTP Location.Please follow below steps to fetch

Step 1 :-  Place 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 credential in config Section

<appSettings> < add key=”FTPPath” value=”ftp://test.com/MyFolder”></add> < add key=”FTPUserName” value=”USERNAME” /> <add key=”FTPPassword” value=”PASSWORD” />

<addkey=”SaveFilePath”value=”C:\HRData\” />

< /appSettings>

<system.net> < defaultProxy useDefaultCredentials=”true”> <proxy autoDetect=”True”/> < /defaultProxy> < /system.net>

Comments

No comments yet. Why don’t you start the discussion?

Leave a Reply