I am trying to create a simple application that allows me to list the files of an FTP server but I have not managed to do it. If someone has a simple way to do it, I would be very grateful.
I am trying to create a simple application that allows me to list the files of an FTP server but I have not managed to do it. If someone has a simple way to do it, I would be very grateful.
In the official documentation you can find an example to make a list of files within an FTP folder, modify the code a bit since it originally uses ListDirectoryDetails and now with ListDirectory gets a "short" list of files:
using System;
using System.IO;
using System.Net;
using System.Text;
namespace Examples.System.Net
{
public class WebRequestGetExample
{
public static void Main ()
{
// Obtiene el objeto que se utiliza para comunicarse con el servidor.
FtpWebRequest request = (FtpWebRequest)WebRequest.Create("ftp://www.contoso.com/");
request.Method = WebRequestMethods.Ftp.ListDirectory;
// Este ejemplo asume que el sitio FTP utiliza autenticación anónima.
request.Credentials = new NetworkCredential ("anonymous","[email protected]");
FtpWebResponse response = (FtpWebResponse)ftpRequest.GetResponse();
StreamReader streamReader = new StreamReader(response.GetResponseStream());
List<string> directories = new List<string>();
string line = streamReader.ReadLine();
//Obtiene el contenido y lo agrega al List<string>.
while (!string.IsNullOrEmpty(line))
{
directories.Add(line);
line = streamReader.ReadLine();
}
streamReader.Close();
//Stream responseStream = response.GetResponseStream();
//StreamReader reader = new StreamReader(responseStream);
//Console.WriteLine(reader.ReadToEnd());
Console.WriteLine("Estatus al listar el contenido del folter {0}", response.StatusDescription);
// reader.Close();
response.Close();
}
}
}