In this article I will explain how to check and find the Internet Connection (Download) speed using C# and VB.Net in ASP.Net.
 
Concept
A file of known size will be downloaded from a remote server 5-10 times and the time required to download the file is noted. Finally the average of these readings will be used to calculate approximate Internet Connection or Download speed.
For more accurate readings, you need to download larger files.
Note: This is a very simple example to make you understand how to find the Internet Connection (Download) speed. But it does not guarantee to be accurate as for accuracy lot of iterations from multiple server locations and are required.
 
 
HTML Markup
The HTML Markup consists of a Label and a Button.
<asp:Label ID="lblDownloadSpeed" runat="server" />
<asp:Button Text="Check Speed" runat="server" OnClick="CheckSpeed" />
 
 
Namespaces
You will need to import the following namespaces.
C#
using System.Net;
using System.Linq;
 
VB.Net
Imports System.Net
Imports System.Linq
 
 
Check Internet Connection (Download) Speed using C# and VB.Net
I am downloading a jQuery script file from Google CDN. The size of the file is 261 KB, which I have determined from windows explorer.
The download speed of calculated by dividing the size of the file (in KB) with the difference in download start and end time (in seconds).
The above process is repeated 5 times and the result is stored in an array after each iteration.
Finally the average of all the 5 readings is displayed in a Label control.
Note: The larger the file the more accurate readings you will get.
C#
protected void CheckSpeed(object sender, EventArgs e)
{
    double[] speeds = new double[5];
    for (int i = 0; i < 5; i++)
    {
        int jQueryFileSize = 261; //Size of File in KB.
        WebClient client = new WebClient();
        DateTime startTime = DateTime.Now;
        client.DownloadFile("http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.js", Server.MapPath("~/jQuery.js"));
        DateTime endTime = DateTime.Now;
        speeds[i] = Math.Round((jQueryFileSize / (endTime - startTime).TotalSeconds));
    }
    lblDownloadSpeed.Text = string.Format("Download Speed: {0}KB/s", speeds.Average());
}
 
VB.Net
Protected Sub CheckSpeed(sender As Object, e As EventArgs)
    Dim speeds As Double() = New Double(4) {}
    For i As Integer = 0 To 4
        Dim jQueryFileSize As Integer = 261
        'Size of File in KB.
        Dim client As New WebClient()
        Dim startTime As DateTime = DateTime.Now
        client.DownloadFile("http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.js", Server.MapPath("~/jQuery.js"))
        Dim endTime As DateTime = DateTime.Now
        speeds(i) = Math.Round((jQueryFileSize / (endTime - startTime).TotalSeconds))
    Next
    lblDownloadSpeed.Text = String.Format("Download Speed: {0}KB/s", speeds.Average())
End Sub
 
 
Screenshot
The following screenshot displays the calculated Internet Connection Download Speed.
Check Internet Connection (Download) Speed using C# and VB.Net in ASP.Net
 
 
Downloads

Download Code