为什么使用c#/FtpWebRequest时Ftp下载速度如此之慢

为什么使用c#/FtpWebRequest时Ftp下载速度如此之慢,c#,.net,ftp,ftpwebrequest,C#,.net,Ftp,Ftpwebrequest,我正在使用c#中的FTP功能来下载一些文件。 我的问题是下载速度慢 我对Filezilla没有任何问题,我的连接也正常 我正在考虑FTP函数的实现,下面是我的代码: public static string DownloadFromList(string strParam,string filePath) { string[] FilesToDownload = strParam.Split('\n'); try {

我正在使用c#中的FTP功能来下载一些文件。 我的问题是下载速度慢

我对Filezilla没有任何问题,我的连接也正常

我正在考虑FTP函数的实现,下面是我的代码:

    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);
        }
    }
  • 是否有必要将useBinary设置为true
  • KeepAlive设置为true是为了使用相同的连接,但是它是真的吗
  • 是否可以只设置一次凭证
请问我该如何改进

非常感谢:)


最好使用探查器,或暂停调试器10次。它最常停在哪里?顺便说一句,先解码为文本,然后立即编码是没有意义的。只需使用Stream.Copy传递字节。我认为这与为每个文件重新创建连接有关。您是如何发现的?然后您可能应该按照我建议的方式进行测量。目前这只是猜测。现在怎么可能有人告诉你问题出在哪里?另外,我希望您对我关于字符串处理的评论做出回应。