Ram Pradeep

Ram Pradeep

  • NA
  • 59
  • 10.7k

downloading with progressbar in win forms

Oct 20 2015 11:51 PM
i have this code for providing progress bar while downloading file but  private void client_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e) event procedure is not getting fired.I am not understanding why it is not fired.please help me to find a way. 
My Code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net;
namespace modalform
{
public partial class Progressbarwithdownload : Form
{
public Progressbarwithdownload()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
WebClient client = new WebClient();
client.DownloadFileAsync(new Uri(@"http://download.services.openoffice.org/files/stable/3.3.0/OOo_3.3.0_Win_x86_install-wJRE_en-US.exe"), "D:\\");
client.DownloadProgressChanged += new DownloadProgressChangedEventHandler(client_DownloadProgressChanged);
client.DownloadFileCompleted += new AsyncCompletedEventHandler(client_DownloadFileCompleted);
button1.Text = "Download In Process";
button1.Enabled = false;
}
private void client_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
{
double bytesIn = double.Parse(e.BytesReceived.ToString());
double totalBytes = double.Parse(e.TotalBytesToReceive.ToString());
double percentage = bytesIn / totalBytes * 100;
progressBar1.Value = int.Parse(Math.Truncate(percentage).ToString());
}
void client_DownloadFileCompleted(object sender, AsyncCompletedEventArgs e)
{
MessageBox.Show("Download Completed");
button1.Text = "Start Download";
button1.Enabled = true;
}
}
}

Answers (2)