How to measure internet speed using C#
How To C Sharp

How to measure internet speed using C#

Mishel Shaji
Mishel Shaji

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
WebClient web = new WebClient();
byte[] bytes = web.DownloadData("");
double sec = watch.Elapsed.TotalSeconds;
double speed = bytes.Count() / sec;
Console.WriteLine(speed +" bytes / S");