Listing all files from ftp server using C#

  • Thread starter Thread starter Aamer Saeed
  • Start date Start date
A

Aamer Saeed

Guest
Hi All,

I want to list all the files from ftp server using c#.........After googled i've got a code that list files from ftp server but just from the top directory or you can say that initial directory but i want to recursively get all the files and the subfolders that are inside the initial directory.How can we do that???????????Can anyone help me??????????Here is the code which i am using for listing files from a single(initial) directory.



private void getFileList(string FTPAddress, string username, string password)
{
List<string> files = new List<string>();

try
{

//Create FTP request
FtpWebRequest request = (FtpWebRequest)FtpWebRequest.Create(FTPAddress);

request.Method = WebRequestMethods.Ftp.ListDirectory;
request.Credentials = new NetworkCredential("anonymous", "anonymous");
request.UsePassive = true;
request.UseBinary = true;
request.KeepAlive = false;


FtpWebResponse response = (FtpWebResponse)request.GetResponse();
Stream responseStream = response.GetResponseStream();
StreamReader reader = new StreamReader(responseStream);

while (!reader.EndOfStream)
{
Application.DoEvents();
files.Add(reader.ReadLine());
}

//Clean-up
reader.Close();
responseStream.Close(); //redundant
response.Close();
}
catch (Exception)
{
MessageBox.Show("There was an error connecting to the FTP Server");
}



//If the list was successfully received, display it to the user
//through a dialog
if (files.Count != 0)
{
foreach (string file in files)
{
listboxFiles.Items.Add(file);
}

}
}


Continue reading...
 
Back
Top