0

I'm using the FTP function in c# in order to download somes files. My problem is that the download is slow.

I don't have any problem with Filezilla, and my connection is OK.

I'm thinking about the FTP functions implementations, here is my code :

    public static string DownloadFromList(string strParam,string filePath)
    {
        string[] FilesToDownload = strParam.Split('\n');

        try
        {
            FtpWebRequest reqFTP;

            foreach(string file in FilesToDownload)
            {
                string tmpfile = file;

                if (tmpfile.Contains("\n"))
                {
                    tmpfile = tmpfile.Replace('\n', ' ');
                }
                FileInfo fileToDownload = new FileInfo(tmpfile);
                tmpfile = fileToDownload.Name;

                FileStream outputStream = new FileStream(filePath.Trim() + "\\" + tmpfile.Trim(), FileMode.Create);

                if (strDirectory.Trim() != "")
                {
                    reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://" + strHost.Trim() + "/" + strDirectory.Trim() + "/" + tmpfile.Trim()));
                }
                else
                {
                    reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://" + strHost.Trim() + "/" + tmpfile.Trim()));
                }

                reqFTP.ConnectionGroupName = "MyGroupName"; 

                reqFTP.Method = WebRequestMethods.Ftp.DownloadFile;
                reqFTP.UseBinary = true;
                reqFTP.KeepAlive = true;
                reqFTP.Timeout = -1;
                reqFTP.Credentials = new NetworkCredential(strUser.Trim(), strPass.Trim());
                FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse();
                Stream ftpStream = response.GetResponseStream();
                StreamReader reader = new StreamReader(ftpStream, System.Text.Encoding.UTF8);
                long cl = response.ContentLength;
                int bufferSize = 8192;
                char[] bytesInStream = new char[8192];
                int readCount;

                StreamWriter sw = new StreamWriter(outputStream);
                int len;
                while ((len = reader.Read(bytesInStream, 0, bytesInStream.Length)) > 0)
                {
                    sw.Write(bytesInStream, 0, len);
                }
                sw.Close();
                outputStream.Close();
                ftpStream.Close();
                response.Close();
            }

            return "ok";
        }
        catch (Exception ex)
        {
            return (ex.Message);
        }
    }
  • Is it necessary to set useBinary = true ?
  • KeepAlive is setted to true in order to use the same connection, but is it true ?
  • Is it possible to set the credential only one time ?

How can i improve this please?

Thanks a lot :)

Best regards,

Walter Fabio Simoni
  • 5,671
  • 15
  • 55
  • 80
  • Use a profiler, or pause the debugger 10 times. Where does it stop most often? Btw, decoding to text and then immediately encoding doesn't make sense. Just pass through the bytes using Stream.Copy. – usr Jun 25 '14 at 10:26
  • I think it's related to recreate a connection for each file – Walter Fabio Simoni Jun 25 '14 at 14:32
  • How did you find out? – usr Jun 25 '14 at 14:33
  • Then you should probably measure like I proposed to be sure. At the moment this is just guessing. How could someone possibly tell you right now what the problem is? Also, I'd expect you to respond to my remarks regarding the string processing. – usr Jun 26 '14 at 10:03

0 Answers0