Code:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Threading;
using System.Diagnostics;
using System.IO;
using System.Net;
namespace Miranda.DownloadProcess
{
public class Form1 : System.Windows.Forms.Form
{
private System.ComponentModel.Container components = null;
private System.Windows.Forms.Button btnStopThread;
private System.Windows.Forms.Button btnStartThread;
private System.Windows.Forms.ProgressBar progressBar1;
private System.Windows.Forms.GroupBox groupBox1;
private System.Windows.Forms.TextBox textBoxURL;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.TextBox textBoxFile;
private System.Windows.Forms.Label textBytesCopied;
private System.Windows.Forms.Label label3;
private System.Windows.Forms.Label textFileSize;
private System.Windows.Forms.Label label4;
private Thread timerThread;
public Form1()
{
InitializeComponent();
}
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.btnStopThread = new System.Windows.Forms.Button();
this.btnStartThread = new System.Windows.Forms.Button();
this.progressBar1 = new System.Windows.Forms.ProgressBar();
this.groupBox1 = new System.Windows.Forms.GroupBox();
this.textBytesCopied = new System.Windows.Forms.Label();
this.label3 = new System.Windows.Forms.Label();
this.textBoxURL = new System.Windows.Forms.TextBox();
this.textBoxFile = new System.Windows.Forms.TextBox();
this.label1 = new System.Windows.Forms.Label();
this.label2 = new System.Windows.Forms.Label();
this.textFileSize = new System.Windows.Forms.Label();
this.label4 = new System.Windows.Forms.Label();
this.groupBox1.SuspendLayout();
this.SuspendLayout();
//
// btnStopThread
//
this.btnStopThread.Location = new System.Drawing.Point(352, 112);
this.btnStopThread.Name = "btnStopThread";
this.btnStopThread.Size = new System.Drawing.Size(133, 23);
this.btnStopThread.TabIndex = 7;
this.btnStopThread.Text = "Abort download";
this.btnStopThread.Click += new System.EventHandler(this.btnStopThread_Click);
//
// btnStartThread
//
this.btnStartThread.Location = new System.Drawing.Point(80, 112);
this.btnStartThread.Name = "btnStartThread";
this.btnStartThread.Size = new System.Drawing.Size(133, 23);
this.btnStartThread.TabIndex = 6;
this.btnStartThread.Text = "Start download";
this.btnStartThread.Click += new System.EventHandler(this.btnStartThread_Click);
//
// progressBar1
//
this.progressBar1.Location = new System.Drawing.Point(16, 24);
this.progressBar1.Name = "progressBar1";
this.progressBar1.Size = new System.Drawing.Size(528, 24);
this.progressBar1.Step = 1;
this.progressBar1.TabIndex = 5;
//
// groupBox1
//
this.groupBox1.Controls.AddRange(new System.Windows.Forms.Control[] {
this.label4,
this.textFileSize,
this.textBytesCopied,
this.btnStopThread,
this.btnStartThread,
this.progressBar1,
this.label3}
);
this.groupBox1.Location = new System.Drawing.Point(24, 128);
this.groupBox1.Name = "groupBox1";
this.groupBox1.Size = new System.Drawing.Size(568, 152);
this.groupBox1.TabIndex = 6;
this.groupBox1.TabStop = false;
this.groupBox1.Text = "Download Progress";
//
// textBytesCopied
//
this.textBytesCopied.Location = new System.Drawing.Point(128, 64);
this.textBytesCopied.Name = "textBytesCopied";
this.textBytesCopied.Size = new System.Drawing.Size(100, 16);
this.textBytesCopied.TabIndex = 8;
//
// label3
//
this.label3.Location = new System.Drawing.Point(32, 64);
this.label3.Name = "label3";
this.label3.Size = new System.Drawing.Size(80, 16);
this.label3.TabIndex = 11;
this.label3.Text = "Bytes Copied:";
//
// textBoxURL
//
this.textBoxURL.Location = new System.Drawing.Point(104, 40);
this.textBoxURL.Name = "textBoxURL";
this.textBoxURL.Size = new System.Drawing.Size(480, 20);
this.textBoxURL.TabIndex = 7;
this.textBoxURL.Text = "";
//
// textBoxFile
//
this.textBoxFile.Location = new System.Drawing.Point(104, 80);
this.textBoxFile.Name = "textBoxFile";
this.textBoxFile.Size = new System.Drawing.Size(480, 20);
this.textBoxFile.TabIndex = 8;
this.textBoxFile.Text = "";
//
// label1
//
this.label1.Location = new System.Drawing.Point(64, 48);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(32, 16);
this.label1.TabIndex = 9;
this.label1.Text = "URL:";
//
// label2
//
this.label2.Location = new System.Drawing.Point(64, 88);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(24, 16);
this.label2.TabIndex = 10;
this.label2.Text = "File:";
//
// textFileSize
//
this.textFileSize.Location = new System.Drawing.Point(416, 64);
this.textFileSize.Name = "textFileSize";
this.textFileSize.Size = new System.Drawing.Size(100, 16);
this.textFileSize.TabIndex = 12;
//
// label4
//
this.label4.Location = new System.Drawing.Point(344, 64);
this.label4.Name = "label4";
this.label4.Size = new System.Drawing.Size(56, 16);
this.label4.TabIndex = 13;
this.label4.Text = "File Size:";
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(624, 325);
this.Controls.AddRange(new System.Windows.Forms.Control[] {
this.label2,
this.label1,
this.textBoxFile,
this.textBoxURL,
this.groupBox1}
);
this.Name = "Form1";
this.Text = "Miranda\'s Download";
this.groupBox1.ResumeLayout(false);
this.ResumeLayout(false);
}
#endregion
// The main entry point for the application.
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
// Start the background thread to update the progress bar
private void btnStartThread_Click(object sender, System.EventArgs e)
{
StopThread();
timerThread = new Thread(new ThreadStart(ThreadProc));
timerThread.IsBackground = true;
timerThread.Start();
}
private void btnStopThread_Click(object sender, System.EventArgs e)
{
StopThread();
}
public void ThreadProc()
{
try
{
MethodInvoker mi = new MethodInvoker(this.UpdateProgress);
int iLength = 1024;
char [] Buffer = new char[1025];
int iBytesRead = 0;
long lTotalBytesRead = 0;
StreamWriter FileWriter=null;
TimeSpan elapsed;
double index = 2.0;
try
{
//Create the request object
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(textBoxURL.Text);
request.AllowAutoRedirect=true;
//Create the response object
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Console.WriteLine("Response Header {0} ", response.Headers);
Console.WriteLine("Response ContentType {0} ", response.ContentType);
long lFileSizeInBytes = Convert.ToInt32(response.ContentLength);
textFileSize.Text = lFileSizeInBytes.ToString("###,###,###,###");
progressBar1.Maximum = 100;
progressBar1.Minimum = 1;
FileInfo TheFile = new FileInfo(textBoxFile.Text);
if (TheFile.Exists)
File.Delete(textBoxFile.Text);
DateTime startTime = DateTime.Now;
FileWriter=new StreamWriter(new FileStream(textBoxFile.Text,FileMode.CreateNew,FileAccess.Write,FileShare.None));
StreamReader sr = new StreamReader(response.GetResponseStream(), System.Text.Encoding.ASCII);
iBytesRead = sr.Read( Buffer, 0, iLength);
FileWriter.Write(Buffer,0, iBytesRead);
lTotalBytesRead += iBytesRead;
while( iBytesRead > 0 )
{
iBytesRead = sr.Read( Buffer, 0, iLength);
FileWriter.Write(Buffer,0, iBytesRead);
lTotalBytesRead += iBytesRead;
textBytesCopied.Text = lTotalBytesRead.ToString("###,###,###,###");
double percent = ((double)lTotalBytesRead /(double)lFileSizeInBytes) * 100.0;
if(percent >= index)
{
index = index+1.0;
this.BeginInvoke(mi);
}
elapsed = DateTime.Now - startTime;
}
elapsed = DateTime.Now - startTime;
MessageBox.Show("Download Completed!","Miranda's Download",
MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}
catch (WebException WebExcp)
{
MessageBox.Show(WebExcp.ToString(), "WebExeption has bee caught!",
MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}
finally
{
if (FileWriter!=null)
{
FileWriter.Close();
}
}
}
catch (ThreadInterruptedException)
{
// Just exit
}
catch (Exception)
{
}
}
private void UpdateProgress()
{
// Reset to start if required
if (progressBar1.Value == progressBar1.Maximum)
{
progressBar1.Value = progressBar1.Minimum ;
}
progressBar1.PerformStep() ;
}
private void StopThread()
{
if (timerThread != null)
{
timerThread.Interrupt();
timerThread = null;
}
}
}
}