How to measure internet speed using C#

In this article, I will show you how to measure internet speed (download) using C#.

We’ll be using Stopwatch and WebClient to calculate internet speed. For using these classes, add references to the following namespaces first.

using System.Diagnostics;
using System.Net;

To measure the internet speed, I created a stopwatch that starts when we start to download a file or web page and ends when the download completes. Then we’re dividing the total number of bytes downloaded with the time taken to download the file to find the download speed in bytes / second.

Stopwatch watch = new Stopwatch(); //using system.diagnostics
watch.Start();
WebClient web = new WebClient();
byte[] bytes = web.DownloadData("https://www.yoursite.com");
watch.Stop();
double sec = watch.Elapsed.TotalSeconds;
double speed = bytes.Count() / sec;
Console.WriteLine(speed +" bytes / S");
Console.ReadKey();

If you enjoyed this post, let me know by leaving a comment below.


Leave a Reply

Your email address will not be published.Required fields are marked *