确定准确的上传速度?

时间:2015-08-18 07:54:37

标签: c# wpf file-upload

我想检查一下系统的上传速度。

void CheckUploadSpeed()
{
    using (var wc = new WebClient())
    {
        IPv4InterfaceStatistics ipis = networkInterface.GetIPv4Statistics();
        BytesSentb4Upload = ipis.BytesSent;
        FileStream stream = File.OpenRead(string.Format("{0}speedtext.txt", path)); //speedtext.txt is a 5 MB file.
        var fileBytes = new byte[stream.Length];
        stream.Read(fileBytes, 0, fileBytes.Length);
        stream.Close();
        startTime = Environment.TickCount; 
        wc.UploadDataAsync(new Uri("http://www.example.com/"), fileBytes);
        InternetSpeedResult = "Data upload started. Uploading 5MB file";
        wc.UploadProgressChanged += new UploadProgressChangedEventHandler(UploadProgressCallback);
        wc.UploadDataCompleted += wc_UploadDataCompleted;
    }
}

并在Upload Progress Changed

void UploadProgressCallback(object sender, UploadProgressChangedEventArgs e)
{
    InternetSpeedResult = string.Format("Checking Upload Speed ... ");
    double endTime = Environment.TickCount; 
    double secs = Math.Round(Math.Floor(endTime - startTime) / 1000, 0); 
    if (secs >= 30) 
    { 
        UploadComplete(sender, e); 
    }
}

这段代码实际上是在解决我的问题,但问题是每次都没有给出确切的结果。因为我指望在特定的时间段内完成BytesSent。这个数字会自动变化。如果速度非常低(小于512 KBPS)且非常高(大于20 MBPS)而不是未达到预期的上传速率。

  1. 我应该在代码中做些什么,我可以依赖于结果?
  2. 是否有其他方法可以检查上传速度
  3. 如果速度非常低(小于512 KBPS)且非常高(大于20 MBPS)。我应该采取什么方法?

0 个答案:

没有答案