Private WebClient mywebclient = new WebClient ();
private void Mywebclient_downloadfilecompleted (object sender, System.ComponentModel.AsyncCompletedEventArgs e)
{
if (e.error! = null)
{
MessageBox.Show (E.error.message, "Information tip", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else if ((e.cancelled = = True))
{
MessageBox.Show ("The download file operation was canceled! "," Information tip ", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
Else
{
MessageBox.Show ("Download file operation complete! "," Information tip ", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
private void Mywebclient_downloadprogresschanged (object sender, System.Net.DownloadProgressChangedEventArgs e)
{
This.progressBar1.Value = E.progresspercentage;
This.label3.Text = "Downloaded" + e.bytesreceived.tostring () + "bytes, all Total" + e.totalbytestoreceive.tostring () + "byte";
}
private void Form1_Load (object sender, EventArgs e)
{
mywebclient.downloadfilecompleted + = new Asynccompletedeventhandler (mywebclient_downloadfilecompleted);
Mywebclient.downloadprogresschanged + = new Downloadprogresschangedeventhandler (mywebclient_ downloadprogresschanged);
}
Start download
private void Button1_Click (object sender, EventArgs e)
{
Try
{
This.progressBar1.Value = 0;
This. Mywebclient.downloadfileasync (New Uri (This.textBox1.Text), this.textBox2.Text);
}
catch (Exception MyEx)
{
MessageBox.Show (Myex.message, "Information tip", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
Cancel Download
private void Button2_Click (object sender, EventArgs e)
{
Mywebclient.cancelasync ();
}
WebClient asynchronous Download file