Kk

Kk

  • NA
  • 20
  • 5.6k

How to get files from ftp folder in windows application c#

Nov 24 2016 7:28 AM
In my application, i use the below code and i am getting only folder names but i need to get folders in the files to download into my apllication folder my  folder name "DownloadFTP".
 
string url = "ftpserverurl";
string ftpUserName = "hai";
string ftpPassWord = "xxxxx";
string localDestnDir = @"C:\Users\Lalitha\Desktop\New folder (4)\DownloadfilesfromFTP\DownloadfilesfromFTP\DownloadFTP";
 
 
private void Download_Click(object sender, EventArgs e)
{
    string[] files = GetFileList();
}
 
 
public string[] GetFileList() // by using this i am getting ftp folders names
{
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;
}
}
 
how can i get folder inside files and i need to down load that.
Please anyone help me out.
Thank you 

Answers (3)