The purpose of this code is the detect how slow is your connection downloading a file from a site. First of all, you have to create a file with a known size: for that you can use fsutil in the prompt (see another post in this blog for info).
When yo do put your file in a webserver (or you can use my url), we can create the code to check the connection speed.
using System; using System.Collections.Generic; using System.Text; using System.Net; namespace SpeedTest { class Program { static void Main(string[] args) { Console.Title = "A simple speed test connection for your app"; // the URL to download a file from Uri URL = new Uri( "https://puresourcecode.com/file.axd?file=/SpeedTest/1024kb.txt" ); WebClient wc = new WebClient(); Console.WriteLine("Simple speedtest"); Console.WriteLine("----------------"); Console.WriteLine("Will test your download rate. " + "Press any key to begin."); Console.ReadKey(); Console.WriteLine("\nDownloading file: 1024kb.txt..."); Console.WriteLine("From https://puresourcecode.com"); Console.WriteLine("Note: This file will automatically " + "be deleted after the test."); // get current tickcount double starttime = Environment.TickCount; // download file from the specified URL, // and save it to C:\speedtest.txt // in your project change the path of the following line wc.DownloadFile(URL, @"C:\speedtest.txt"); // get current tickcount double endtime = Environment.TickCount; // how many seconds did it take? // we are calculating this by subtracting starttime from // endtime and dividing by 1000 (since the tickcount is in // miliseconds 1000 ms = 1 sec) double secs = Math.Floor(endtime - starttime) / 1000; // calculate download rate in kb per sec. // this is done by dividing 1024 by the number of seconds it // took to download the file (1024 bytes = 1 kilobyte) double kbsec = Math.Round(1024 / secs); Console.WriteLine("\nCompleted. Statistics:\n"); Console.WriteLine("1mb download: \t{0} secs", secs); Console.WriteLine("Download rate: \t{0} kb/sec", kbsec); Console.WriteLine("\nPress any key to exit..."); Console.Read(); Console.WriteLine("Deleting file..."); try { // delete downloaded file System.IO.File.Delete(@"C:\speedtest.txt"); Console.WriteLine("Done."); } catch { Console.WriteLine("Couldn't delete download file."); Console.WriteLine("To delete the file yourself."); Console.ReadKey(); } } } }
Happy coding!