Click here to Skip to main content
15,884,176 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
C#
using(WebClient webClient = new WebClient())
             {
             MessageBox.Show("inside webclient***");
             webClient.DownloadFile(file, temp_cmp);  // this statement will download the content url/file into the filecreated temp_cmp
             webClient.DownloadFileCompleted += new System.ComponentModel.AsyncCompletedEventHandler(Completed);
             webClient.DownloadDataCompleted +=new DownloadDataCompletedEventHandler(Completed);
             webClient.DownloadFileCompleted += new System.ComponentModel.AsyncCompletedEventHandler(webClient_DownloadFileCompleted);
             MessageBox.Show("after webclient.downloadfile()");
             Thread.Sleep(1000);
             }


and following are my event handlers. i wana try all possibilities, thats why, i wrote many of them

C#
 void webClient_DownloadFileCompleted(object sender, AsyncCompletedEventArgs e)
      {
 MessageBox.Show("Download Completed");
 throw new NotImplementedException();
  }
void Completed(object sender, AsyncCompletedEventArgs e)
  {
 MessageBox.Show("Download Completed");
 MessageBox.Show(e.ToString());
  }
Posted

Events attachment is written after the "DownloadFile".

you may use:

C#
private void btnDownload_Click(object sender, EventArgs e)
{
  WebClient webClient = new WebClient();
  webClient.DownloadFileCompleted += new AsyncCompletedEventHandler(Completed);
  webClient.DownloadFileAsync(new Uri("http://mysite.com/myfile.txt"), @"c:\myfile.txt");
}

private void Completed(object sender, AsyncCompletedEventArgs e)
{
  MessageBox.Show("Download completed!");
}
 
Share this answer
 
Comments
hina_abbas123 25-Nov-13 4:44am    
i am doing the same thing
check the order of line with method of webclient.

1. you are using "DownloadFile", but in provide code "DownloadFileAsync" for async (usually downloading function are async).
2. you have fire "DownloadFile" this method at start with out attaching the events.

first attach the events than fire it
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900