private static void DownloadFileComplete(Object sender, AsyncCompletedEventArgs e) { FileDownloadResults results = e.UserState as FileDownloadResults; if (e.Cancelled || e.Error != null) { results.downloadComplete.Set(); return; } results.fileOk = true; results.downloadComplete.Set(); }
public static bool DownloadFile(string fileUrl, string dstName, DownloadProgressChangedEventHandler callback) { WebClient client = new WebClient(); // Add a user agent header in case the // requested URI contains a query. client.Headers.Add("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.0.3705;)"); client.DownloadFileCompleted += DownloadFileComplete; client.DownloadProgressChanged += callback; FileDownloadResults res = new FileDownloadResults(); client.DownloadFileAsync(new Uri(fileUrl), dstName, res); res.downloadComplete.WaitOne(); return(res.fileOk); }