So I'm trying to download two images and just for demonstration I've checked their sizes and summed it up to a variable called totalBytes
.
I want to keep track of how much has been downloaded out of those total bytes so I can calculate the percentage by taking downloaded
/ totalBytes * 100
But I have no idea how to keep track of the mount of bytes that has been downloaded.
public static int totalBytes = 1378954;
static void Main(string[] args)
{
var images = new List<string>
{
"http://4.bp.blogspot.com/-HTvSYzA-pO4/UgQb4Zh_u0I/AAAAAAAAEuI/XwhtogT_1tA/s1600/3+cute2.jpg",
"http://getwallpapers.com/wallpaper/full/7/7/0/74728.jpg"
};
foreach (var image in images)
{
int i = 0;
using (var wc = new WebClient())
{
wc.DownloadProgressChanged += Wc_DownloadProgressChanged;
wc.DownloadFileAsync(new Uri(image), $"image{i}.png");
i++;
Console.ReadKey();
}
}
}
private static void Wc_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
{
Console.WriteLine($"Downloaded: {e.BytesReceived} out of {totalBytes}");
}