Skip to main content

WPF C#/VB
Calculate Network Speed



C#

// AZUL CODING ---------------------------------------
// WPF C#/VB - Calculate Network Speed
// https://youtu.be/x3v5c_1C7r0


using System;
using System.Linq;
using System.Net;
using System.IO;
using System.Windows;
using System.Net.NetworkInformation;
using SpeedTest.Net;

namespace NetworkSpeed
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();

            // Choose your preferred method...
            // CheckSpeedFastPlugin();
            // CheckSpeedNetworkInterface();
            // CheckSpeedWebClient();
        }

        public async void CheckSpeedFastPlugin()
        {
            var speed = await FastClient.GetDownloadSpeed(SpeedTest.Net.Enums.SpeedTestUnit.MegaBitsPerSecond);
            SpeedTxt.Text = Math.Round(speed.Speed, 2).ToString();
        }

        public void CheckSpeedNetworkInterface()
        {
            NetworkInterface[] adapters = NetworkInterface.GetAllNetworkInterfaces();
            foreach (NetworkInterface adapter in adapters)
            {
                // Change this to the name of your Internet adapter
                if (adapter.Description.Contains("802.11ac"))
                {
                    SpeedTxt.Text = Math.Round(adapter.Speed / 10000000d, 2).ToString();
                    break;
                }
            }
        }

        public void CheckSpeedWebClient()
        {
            // Change the output directory (test.js) to your own
            double[] speeds = new double[5];
            for (int i = 0; i < 5; i++)
            {
                int jQueryFileSize = 261;
                WebClient client = new WebClient();
                DateTime startTime = DateTime.Now;
                client.DownloadFile("http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.js", @"test.js");
                DateTime endTime = DateTime.Now;
                speeds[i] = Math.Round(jQueryFileSize / (endTime - startTime).TotalSeconds, 2);
            }
            File.Delete(@"test.js");
            SpeedTxt.Text = Math.Round(speeds.Average() * 0.008f, 2).ToString();
        }
    }
}

Enjoying this tutorial?


VB.NET

' AZUL CODING ---------------------------------------
' WPF C#/VB - Calculate Network Speed
' https://youtu.be/x3v5c_1C7r0


Imports System
Imports System.Linq
Imports System.Net
Imports System.IO
Imports System.Windows
Imports System.Net.NetworkInformation
Imports SpeedTest.Net

Class MainWindow

    Public Sub New()
        InitializeComponent()
        
        ' Choose your preferred method...
        ' CheckSpeedFastPlugin()
        ' CheckSpeedNetworkInterface()
        ' CheckSpeedWebClient()
    End Sub

    Public Async Sub CheckSpeedFastPlugin()
        Dim speed = Await FastClient.GetDownloadSpeed(SpeedTest.Net.Enums.SpeedTestUnit.MegaBitsPerSecond)
        SpeedTxt.Text = Math.Round(speed.Speed, 2).ToString()
    End Sub

    Public Sub CheckSpeedNetworkInterface()
        Dim adapters As NetworkInterface() = NetworkInterface.GetAllNetworkInterfaces()

        For Each adapter As NetworkInterface In adapters
            ' Change this to the name of your Internet adapter
            If adapter.Description.Contains("802.11ac") Then
                SpeedTxt.Text = Math.Round(adapter.Speed / 10000000R, 2).ToString()
                Exit For
            End If
        Next
    End Sub

    Public Sub CheckSpeedWebClient()
        Dim speeds As Double() = New Double(4) {}

        ' Change the output directory (test.js) to your own
        For i As Integer = 0 To 5 - 1
            Dim jQueryFileSize As Integer = 261
            Dim client As WebClient = New WebClient()
            Dim startTime As DateTime = DateTime.Now
            client.DownloadFile("http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.js", "test.js")
            Dim endTime As DateTime = DateTime.Now
            speeds(i) = Math.Round(jQueryFileSize / (endTime - startTime).TotalSeconds, 2)
        Next

        File.Delete("test.js")
        SpeedTxt.Text = Math.Round(speeds.Average() * 0.008F, 2).ToString()
    End Sub

End Class

XAML

<!-- AZUL CODING --------------------------------------- -->
<!-- WPF C#/VB - Calculate Network Speed -->
<!-- https://youtu.be/x3v5c_1C7r0 -->


<Window x:Class="NetworkSpeed.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:NetworkSpeed"
        mc:Ignorable="d"
        Title="Network Speed Test" Width="364" ResizeMode="CanMinimize" SizeToContent="Height" WindowStartupLocation="CenterScreen">
    <StackPanel>
        <TextBlock x:Name="SpeedTxt" Text="•••" Margin="20,30,20,0" FontSize="72" VerticalAlignment="Stretch" HorizontalAlignment="Center" FontWeight="SemiBold"/>
        <TextBlock Text="Mbps" FontSize="16" HorizontalAlignment="Center" Margin="0,0,0,50"/>
    </StackPanel>
</Window>