public void Download() { try { // Construct the temporary file path. string tempPath = tbPath.Text.Trim() + ".tmp"; // Delete the temporary file if it already exists. if (File.Exists(tempPath)) { File.Delete(tempPath); } // Initialize an instance of HttpDownloadClient. // Store the file to a temporary file first. client = new HttpDownloadClient(tbURL.Text, tempPath); // Register the events of HttpDownloadClient. client.DownloadCompleted += new EventHandler <HttpDownloadCompletedEventArgs>( DownloadCompleted); client.DownloadProgressChanged += new EventHandler <HttpDownloadProgressChangedEventArgs>(DownloadProgressChanged); client.StatusChanged += new EventHandler(StatusChanged); // Start to download file. client.Start(); } catch (Exception ex) { MessageBox.Show(ex.Message); } }
/// <summary> /// Handle btnDownload Click event. /// </summary> private void btnDownload_Click(object sender, EventArgs e) { try { // Check whether the file exists. if (File.Exists(tbPath.Text.Trim())) { string message = "There is already a file with the same name, " + "do you want to delete it? " + "If not, please change the local path. "; var result = MessageBox.Show( message, "File name conflict: " + tbPath.Text.Trim(), MessageBoxButtons.OKCancel); if (result == System.Windows.Forms.DialogResult.OK) { File.Delete(tbPath.Text.Trim()); } else { return; } } // Construct the temporary file path. string tempPath = tbPath.Text.Trim() + ".tmp"; // Delete the temporary file if it already exists. if (File.Exists(tempPath)) { File.Delete(tempPath); } // Initialize an instance of HttpDownloadClient. // Store the file to a temporary file first. client = new HttpDownloadClient(tbURL.Text, tempPath); // Register the events of HttpDownloadClient. client.DownloadCompleted += new EventHandler <HttpDownloadCompletedEventArgs>( DownloadCompleted); client.DownloadProgressChanged += new EventHandler <HttpDownloadProgressChangedEventArgs>(DownloadProgressChanged); client.StatusChanged += new EventHandler(StatusChanged); // Start to download file. client.Start(); } catch (Exception ex) { MessageBox.Show(ex.Message); } }