private void startDownload()
{
WebClient client = new WebClient();
client.DownloadProgressChanged += new DownloadProgressChangedEventHandler(client_DownloadProgressChanged);
client.DownloadFileCompleted += new AsyncCompletedEventHandler(client_DownloadFileCompleted);
client.DownloadFileAsync(new Uri("https://github.com/JPPlaysGamer/IndustriesExes.Inc/releases/download/ieip-pre2/IEIPv0.2-alpha-Windows.zip"), temp + "\\IEIPv0.2-alpha-Windows.zip");
}
void client_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
{
this.BeginInvoke((MethodInvoker)delegate {
double bytesIn = double.Parse(e.BytesReceived.ToString());
double totalBytes = double.Parse(e.TotalBytesToReceive.ToString());
double percentage = bytesIn / totalBytes * 100;
Mbs.Text = (bytesIn / 1024 / 1024).ToString("F2") + " / " + (totalBytes / 1024/ 1024).ToString("F2") + " Mb";
Complete.Text = "Status: Downloading...";
IEIPProgress.Value = int.Parse(Math.Truncate(percentage).ToString());
this.Text = "IEIP Installer - " + IEIPProgress.Value + "%";
});
}
void client_DownloadFileCompleted(object sender, AsyncCompletedEventArgs e)
{
this.BeginInvoke((MethodInvoker)delegate {
if(File.Exists(temp + "\\IEIPv0.2-alpha-Windows.zip"))
{
File.Move(temp + "\\IEIPv0.2-alpha-Windows.zip", localProg + "\\IEIPv0.2-alpha-Windows.zip");
}
Complete.Text = "Status: Completed";
btnNo.Text = "Exit";
btnNo.Enabled = true;
});
}
See this code on the Downloadfileasync. As this method does not block main thread you can create two events mentioned above.
To do this percentage, you must first create a variable to receive the received bytes, total file size, and for the percentage
void client_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
{
this.BeginInvoke((MethodInvoker)delegate {
double bytesIn = double.Parse(e.BytesReceived.ToString());
double totalBytes = double.Parse(e.TotalBytesToReceive.ToString());
double percentage = bytesIn / totalBytes * 100;
Mbs.Text = (bytesIn / 1024 / 1024).ToString("F2") + " / " + (totalBytes / 1024/ 1024).ToString("F2") + " Mb";
Complete.Text = "Status: Downloading...";
IEIPProgress.Value = int.Parse(Math.Truncate(percentage).ToString());
this.Text = "IEIP Installer - " + IEIPProgress.Value + "%";
});
}
As this example is of a file of mine it is of many MB so I do a division of bytes received by 1024, the size of KB, MB, GB , ...
I do it twice because I want to show a megabyte but if your item is Kilobyte only once. Then make received bytes divided by total bytes times 100, when adding this value to the progressibar use Math.Truncate(percentage). Use toString to make your decimal formatted. Example: toString("F2"), this indicates that it will have two decimal places.
In your form (or console), make your design you like best and prefer. Finally create an event if the download was complete:
void client_DownloadFileCompleted(object sender, AsyncCompletedEventArgs e)
{
this.BeginInvoke((MethodInvoker)delegate {
if(File.Exists(temp + "\\IEIPv0.2-alpha-Windows.zip"))
{
File.Move(temp + "\\IEIPv0.2-alpha-Windows.zip", localProg + "\\IEIPv0.2-alpha-Windows.zip");
}
Complete.Text = "Status: Completed";
btnNo.Text = "Exit";
btnNo.Enabled = true;
});
}
If your download was successful create an if to check if the file has the same bytes as the one in the URL. If you don’t have this size create an error message. Do a good file control or better create a temp.