List FTP Server Files

3

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.

    
asked by Edwin V 05.06.2016 в 22:18
source

1 answer

2

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();
            }
        }
    }

How to: Show the directory content with FTP (English)

    
answered by 05.06.2016 / 22:40
source