Here is the code snippet to check your internet connection speed (download speed).
Add namespace
using System.Net;
Write code in a Web application on a button click event handler.
  1. Uri URL = new Uri("http://sixhoej.net/speedtest/1024kb.txt");
  2. WebClient wc = new WebClient();
  3. double starttime = Environment.TickCount;
  4. // download file from the specified URL, and save it to C:\speedtest.txt
  5. wc.DownloadFile(URL, @"C:\speedtest.txt");
  6. // get current tickcount
  7. double endtime = Environment.TickCount;
  8. // how many seconds did it take?
  9. // we are calculating this by subtracting starttime from endtime
  10. // and dividing by 1000 (since the tickcount is in miliseconds.. 1000 ms = 1 sec)
  11. double secs = Math.Floor(endtime - starttime) / 1000;
  12. // round the number of secs and remove the decimal point
  13. double secs2 = Math.Round(secs, 0);
  14. // calculate download rate in kb per sec.
  15. // this is done by dividing 1024 by the number of seconds it
  16. // took to download the file (1024 bytes = 1 kilobyte)
  17. double kbsec = Math.Round(1024 / secs);
  18. Label1.Text = "Download rate: " + kbsec + " kb/sec";
  19. try
  20. {
  21. // delete downloaded file
  22. System.IO.File.Delete(@"C:\speedtest.txt");
  23. Response.Write("Done.");
  24. }
  25. catch
  26. {
  27. Response.Write("Couldn't delete download file.");
  28. Response.Write("To delete the file yourself, go to your C-drive and look for the file 'speedtest.txt'.");
  29. }