Read file from an FTP server without downloading them to my disk

0

I have an app that is reading CSV files from an FTP server and downloads it to a folder on the disk

 private static void DownloadFtpDirectory(string url, NetworkCredential credentials, string localPath)
        {
            ServicePointManager.ServerCertificateValidationCallback = AcceptAllCertifications;
            FtpWebRequest listRequest = (FtpWebRequest)WebRequest.Create(url);
            listRequest.EnableSsl = true;
            listRequest.Method = WebRequestMethods.Ftp.ListDirectoryDetails;
            listRequest.Credentials = credentials;

            List<string> lines = new List<string>();

            using (FtpWebResponse listResponse = (FtpWebResponse)listRequest.GetResponse())
            using (Stream listStream = listResponse.GetResponseStream())
            using (StreamReader listReader = new StreamReader(listStream))
            {
                while (!listReader.EndOfStream)
                {
                    lines.Add(listReader.ReadLine());
                }
            }

            foreach (string line in lines)
            {
                string[] tokens =
                    line.Split(new[] { ' ' }, 9, StringSplitOptions.RemoveEmptyEntries);
                string name = tokens[8];
                string permissions = tokens[0];

                string localFilePath = Path.Combine(localPath, name);
                string fileUrl = url + "/" + name;

                if (permissions[0] == 'd')
                {
                    if (!Directory.Exists(localFilePath))
                    {
                        Directory.CreateDirectory(localFilePath);
                    }

                    DownloadFtpDirectory(fileUrl + "/", credentials, localFilePath);
                }
                else
                {
                    FtpWebRequest downloadRequest = (FtpWebRequest)WebRequest.Create(fileUrl);
                    downloadRequest.Method = WebRequestMethods.Ftp.DownloadFile;
                    downloadRequest.Credentials = credentials;

                    using (FtpWebResponse downloadResponse =
                              (FtpWebResponse)downloadRequest.GetResponse())
                    using (Stream sourceStream = downloadResponse.GetResponseStream())
                    using (Stream targetStream = File.Create(localFilePath))
                    {
                        byte[] buffer = new byte[10240];
                        int read;
                        while ((read = sourceStream.Read(buffer, 0, buffer.Length)) > 0)
                        {
                            targetStream.Write(buffer, 0, read);
                        }
                    }
                }
            }
        }

Then I read the files on the disk and put them in a list

  using (var reader = new StreamReader(@"C:\xxx.csv"))
            {
                List<string> listA = new List<string>();
                while (!reader.EndOfStream)
                {
                    var line = reader.ReadLine();
                    var values = line.Split(';');

                    listA.Add(values[0]);

                }
            }

My question: Can I read the files without having to download them to my disk?

    
asked by Za7pi 04.08.2017 в 11:28
source

1 answer

1

With this method you do not have to record the file locally, you send it directly from the stream reader to the List:

WebRequest request = FtpWebRequest.Create(url);
using (WebResponse response = request.GetResponse())
{
    Stream responseStream = response.GetResponseStream();
    Encoding encode = System.Text.Encoding.GetEncoding("utf-8");
    using( var reader = new StreamReader( responseStream, encode ))
    {
        List<string> listA = new List<string>();
        while (!reader.EndOfStream)
        {
            var line = reader.ReadLine();
            var values = line.Split(';');
            listA.Add(values[0]);
        }
    }
}
    
answered by 04.08.2017 в 13:28