我想检查一下系统的上传速度。
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)而不是未达到预期的上传速率。