using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using System.Net; using System.IO; using System.Threading; namespace sDownQVGA { public partial class sDown : Form { private string path = Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase); private About _about = new About(); delegate void SetTextCallback(string text); public sDown() { InitializeComponent(); } private void DownloadFile(String remoteFilename, String localFilename) { // Function will return the number of bytes processed // to the caller. Initialize to 0 here. int bytesProcessed = 0; // Assign values to these objects here so that they can // be referenced in the finally block Stream remoteStream = null; Stream localStream = null; WebResponse response = null; // Use a try/catch/finally block as both the WebRequest and Stream // classes throw exceptions upon error try { // Create a request for the specified remote file name WebRequest request = WebRequest.Create(remoteFilename); if (request != null) { debug.Text = "Connecting..."; debug.Refresh(); // Send the request to the server and retrieve the // WebResponse object response = request.GetResponse(); if (response != null) { debug.Text = "Downloading..."; debug.Refresh(); // Once the WebResponse object has been retrieved, // get the stream object associated with the response's data remoteStream = response.GetResponseStream(); // Create the local file localStream = File.Create(localFilename); // Allocate a 1k buffer byte[] buffer = new byte[1024]; int bytesRead; progressBar1.Visible = true; progressBar1.Refresh(); rate.Visible = true; rate.Refresh(); // Simple do/while loop to read from stream until // no bytes are returned do { // Read data (up to 1k) from the stream bytesRead = remoteStream.Read(buffer, 0, buffer.Length); // Write the data to the local file localStream.Write(buffer, 0, bytesRead); // Increment total bytes processed bytesProcessed += bytesRead; UpdateProgress(bytesProcessed, response.ContentLength); } while (bytesRead > 0); UpdateProgress(0, 0); rate.Visible = false; rate.Refresh(); progressBar1.Visible = false; progressBar1.Refresh(); debug.Text = "Success! " + bytesProcessed + " bytes written."; } } } catch (Exception e) { MessageBox.Show(e.Message); debug.Text = ""; } finally { // Close the response and streams objects here // to make sure they're closed even if an exception // is thrown at some point if (response != null) response.Close(); if (remoteStream != null) remoteStream.Close(); if (localStream != null) localStream.Close(); } } private void UpdateProgress(int value, long max) { value = value / 1000; max = max / 1000; progressBar1.Minimum = 0; progressBar1.Maximum = (int)max; progressBar1.Value = value; rate.Text = "" + value + "kB / " + Convert.ToInt64(max) + "kB"; rate.Refresh(); } private void BeginDownload() { string[] split = SetSaveAs(textBoxURL.Text.Split('/')); string tmp = split[(split.Length - 1)]; if (textBoxSaveAs.Text == "") { DownloadFile(textBoxURL.Text, path + "\\" + tmp); } else { DownloadFile(textBoxURL.Text, path + "\\" + textBoxSaveAs.Text); } } private void textBoxSaveAs_GotFocus(object sender, EventArgs e) { if (textBoxSaveAs.Text == "") { string[] split = textBoxURL.Text.Split('/'); textBoxSaveAs.Text = split[(split.Length - 1)]; } } private void menuItem3_Click(object sender, EventArgs e) { _about.Show(); } private void menuItem4_Click(object sender, EventArgs e) { Application.Exit(); } private void menuItem2_Click(object sender, EventArgs e) { string[] split = textBoxURL.Text.Split('/'); string tmp = split[(split.Length - 1)]; textBoxSaveAs.Text = tmp; new Thread(new ThreadStart(BeginDownload)).Start(); } private void SetSaveAs(string text) { if (textBoxSaveAs.InvokeRequired) { SetTextCallback d = new SetTextCallback(SetSaveAs); this.Invoke(d, new object[] { text }); } else { string[] split = textBoxURL.Text.Split('/'); string tmp = split[(split.Length - 1)]; } } } }