C# – How to Create a Download speed test with .NET


I'd like to create a speed test to test the connection.

What I would like is a 15sec download which then gives me the average download speed.

Anyone knows how to create this? or has a better idea to make a speed test?

Best Solution

This sample will try to download googletalk, and then outputs details of the download.

ps. when trying to time and operation avoid using DateTime as they can cause problems or inacurecy, always use Stopwatch available at System.Diagnostics namespace.

const string tempfile = "tempfile.tmp";
System.Net.WebClient webClient = new System.Net.WebClient();

Console.WriteLine("Downloading file....");

System.Diagnostics.Stopwatch sw = System.Diagnostics.Stopwatch.StartNew();
webClient.DownloadFile("http://dl.google.com/googletalk/googletalk-setup.exe", tempfile);

System.IO.FileInfo fileInfo = new System.IO.FileInfo(tempfile);
long speed = fileInfo.Length / sw.Elapsed.Seconds;

Console.WriteLine("Download duration: {0}", sw.Elapsed);
Console.WriteLine("File size: {0}", fileInfo.Length.ToString("N0"));
Console.WriteLine("Speed: {0} bps ", speed.ToString("N0"));

Console.WriteLine("Press any key to continue...");