Skip to main content
 首页 » 编程设计

c#之我正在尝试获得 ftp 下载文件的速度,但有时我变得无穷大,为什么

2024年02月06日15powertoolsteam

这是计算下载速度的方法:

private void CalculateDownload(int ln, BackgroundWorker backw) 
{ 
    string speed; 
    DateTime DownloadStart = DateTime.Now; 
 
    int dnsize = ln; 
 
    if (dnsize > 0) 
    { 
        DateTime DownloadEnd = DateTime.Now; 
        TimeSpan DownloadSub = DownloadEnd.Subtract( 
            DownloadStart); 
 
        speed = string.Format("Download speed: {0:F0} bps\n", 
            (dnsize / DownloadSub.TotalMilliseconds) * 1000); 
        backw.ReportProgress(0,speed); 
    } 
} 

这是我用来从 ftp 服务器下载文件的方法。每次一个文件。

public int FtpDownload(object sender, string file, string filesdirectories, string fn) 
{ 
    string tmp = ""; 
 
    BackgroundWorker bw = sender as BackgroundWorker; 
    string filenameonly = Path.GetFileName(file); 
    string ftpdirectories = Path.Combine(ftpcontentdir, filesdirectories); 
    string fileurl = "ftp://" + file; 
    FtpWebRequest reqFTP; 
    reqFTP = (FtpWebRequest)FtpWebRequest.Create(fileurl); 
    reqFTP.Credentials = new NetworkCredential(UserName, Password); 
    reqFTP.UseBinary = true; 
    reqFTP.UsePassive = true; 
    reqFTP.KeepAlive = true; 
 
    reqFTP.Method = WebRequestMethods.Ftp.DownloadFile; 
    try 
    { 
        FtpWebResponse response = (FtpWebResponse)reqFTP. 
            GetResponse(); 
 
        Stream responseStream = response.GetResponseStream(); 
        StreamReader reader = new StreamReader(responseStream); 
        tmp = reader.ReadToEnd(); 
        CalculateDownload(tmp.Length, bw); 
        reader.Close(); 
        response.Close(); 
    } 
    catch (WebException e) 
    { 
        Console.WriteLine(e.ToString()); 
    } 
 
    return tmp.Length; 
} 

我获取文件大小的 CalculateDownload 方法中的第一个文件下载:

变量 ln 例如 66349

然后可变速度包含:下载速度:21367 bps 在下一个文件中:

ln = 59892

则速度为:下载速度:25100 bps

当我使用断点时工作正常,但如果我不使用断点而不是 25100 bps,我将看到无限 bps

在 form1 中,我发送要下载的文件是这样的:

private void backgroundWorker2_DoWork(object sender, DoWorkEventArgs e) 
{ 
    for (int i = 0; i < numberOfFiles.Count; i++) 
    { 
        int fn = numberOfFiles[i].IndexOf(txtHost.Text, 0); 
        string fn1 = numberOfFiles[i].Substring(txtHost.Text.Length + 1, numberOfFiles[i].Length - (txtHost.Text.Length + 1)); 
        string dirs = Path.GetDirectoryName(fn1); 
        string filename = Path.GetFileName(fn1); 
        ftpProgress1.FtpDownload(sender, numberOfFiles[i], dirs, filename); 
    } 
} 

并且 form1 中的进度更改事件:

private void backgroundWorker2_ProgressChanged(object sender, ProgressChangedEventArgs e) 
{            
    this.toolStripStatusLabel1.Text = e.UserState.ToString(); 
} 

为什么当我使用断点时它显示每个文件的下载速度但没有断点它显示无穷大?

请您参考如下方法:

当使用 float 或 double 时,除以 0 得出无穷大。

你可以添加一个额外的检查来查看是否已经过了 0 毫秒 在那种情况下不计算该值。在 Time = 0 时,因为没有下载任何内容 但是该值可能对用户没有用。

您还可以通过传递一个实现接口(interface)的类来添加单元测试 模拟调用 back.ReportProgress 的方法,例如:

private void CalculateDownload(int ln, IBackgroundWorker backw) 

弄清楚在 Time = 0、Time = 1 等处发生了什么。

最后一行:

int dnsize = ln; 

没有用,因为您已经将变量传递给您的函数