///// <summary> ///// Begin downloading the file at the specified url, and save it to the current fileName. ///// </summary> //public void Download(string url) //{ // Download(url, ""); //} /// <summary> /// Begin downloading the file at the specified url, and save it to the given fileName. /// </summary> public void Download(string url, string hostName, string destFileName, int timeout) { DownloadData data = null; this.canceled = false; try { // get download details data = DownloadData.Create(url, hostName, destFileName, timeout); //// Find out the name of the file that the web server gave us. //string destFileName = Path.GetFileName(data.Response.ResponseUri.ToString()); // create the download buffer byte[] buffer = new byte[downloadBlockSize]; int readCount; // update how many bytes have already been read long totalDownloaded = data.StartPoint; bool gotCanceled = false; while ((int)(readCount = data.DownloadStream.Read(buffer, 0, downloadBlockSize)) > 0) { // break on cancel if (this.canceled) { gotCanceled = true; break; } // update total bytes read totalDownloaded += readCount; // save block to end of file SaveToFile(buffer, readCount, data.DownloadingToStream); // send progress info if (data.IsProgressKnown) { RaiseProgressChanged(totalDownloaded, data.FileSize); } // break on cancel if (this.canceled) { gotCanceled = true; break; } } // stream could be incomplete if (data.IsProgressKnown) { if (totalDownloaded < data.FileSize) { throw new WebException("date transfer not completed", WebExceptionStatus.ConnectionClosed); } } if (!gotCanceled) { RaiseProgressChanged(data.FileSize, data.FileSize); data.CleanOnFinish(); OnDownloadComplete(); } } catch (UriFormatException e) { throw new ArgumentException( String.Format("Could not parse the URL \"{0}\" - it's either malformed or is an unknown protocol.", url), e); } finally { if (data != null) { data.Close(); } } }