www.pudn.com > SharpDownload(FTP¼°WEBÏÂÔØ).zip > Form1.cs


using System; 
using System.Drawing; 
using System.Collections; 
using System.ComponentModel; 
using System.Windows.Forms; 
using System.Data; 
 
using System.Diagnostics; 
 
namespace SharpDownload 
{ 
	///  
	/// Summary description for Form1. 
	///  
	public class Form1 : System.Windows.Forms.Form 
	{ 
		private System.Windows.Forms.Label label2; 
		private System.Windows.Forms.Button button1; 
		private System.Windows.Forms.Label label1; 
		private System.Windows.Forms.TextBox textBox1; 
		private System.Windows.Forms.TextBox textBox2; 
		private System.Windows.Forms.ProgressBar progressBar1; 
		///  
		/// Required designer variable. 
		///  
		private System.ComponentModel.Container components = null; 
 
		private FileDownload fileDown; 
		private ProgressBar[] progressBar; 
		private Label[] progressLabel; 
 
		private const long MIN_SPLIT_SIZE = 20000; 
		private System.Windows.Forms.ProgressBar progressBar2; 
		private System.Windows.Forms.ProgressBar progressBar3; 
		private System.Windows.Forms.ProgressBar progressBar4; 
		private System.Windows.Forms.Label label3; 
		private System.Windows.Forms.Label label4; 
		private System.Windows.Forms.Label label5; 
		private System.Windows.Forms.Label label6; 
		private const int MAX_FILE_PARTS = 4; 
 
		public Form1() 
		{ 
			// 
			// Required for Windows Form Designer support 
			// 
			InitializeComponent(); 
 
			// 
			// TODO: Add any constructor code after InitializeComponent call 
			// 
		} 
 
		///  
		/// Clean up any resources being used. 
		///  
		protected override void Dispose( bool disposing ) 
		{ 
			if( disposing ) 
			{ 
				if (components != null)  
				{ 
					components.Dispose(); 
				} 
			} 
			base.Dispose( disposing ); 
		} 
 
		private void InitializeComponent() { 
			this.progressBar1 = new System.Windows.Forms.ProgressBar(); 
			this.textBox2 = new System.Windows.Forms.TextBox(); 
			this.textBox1 = new System.Windows.Forms.TextBox(); 
			this.label1 = new System.Windows.Forms.Label(); 
			this.button1 = new System.Windows.Forms.Button(); 
			this.label2 = new System.Windows.Forms.Label(); 
			this.progressBar2 = new System.Windows.Forms.ProgressBar(); 
			this.progressBar3 = new System.Windows.Forms.ProgressBar(); 
			this.progressBar4 = new System.Windows.Forms.ProgressBar(); 
			this.label3 = new System.Windows.Forms.Label(); 
			this.label4 = new System.Windows.Forms.Label(); 
			this.label5 = new System.Windows.Forms.Label(); 
			this.label6 = new System.Windows.Forms.Label(); 
			this.SuspendLayout(); 
			//  
			// progressBar1 
			//  
			this.progressBar1.Location = new System.Drawing.Point(8, 168); 
			this.progressBar1.Name = "progressBar1"; 
			this.progressBar1.Size = new System.Drawing.Size(336, 16); 
			this.progressBar1.TabIndex = 4; 
			//  
			// textBox2 
			//  
			this.textBox2.Location = new System.Drawing.Point(8, 64); 
			this.textBox2.Name = "textBox2"; 
			this.textBox2.Size = new System.Drawing.Size(328, 20); 
			this.textBox2.TabIndex = 2; 
			this.textBox2.Text = "c:\\\\"; 
			//  
			// textBox1 
			//  
			this.textBox1.Location = new System.Drawing.Point(8, 24); 
			this.textBox1.Name = "textBox1"; 
			this.textBox1.Size = new System.Drawing.Size(328, 20); 
			this.textBox1.TabIndex = 0; 
			this.textBox1.Text = "ftp://ftp.cdrom.com/pub/simtelnet/winxp/arcade/simon32.zip"; 
			//  
			// label1 
			//  
			this.label1.Location = new System.Drawing.Point(8, 8); 
			this.label1.Name = "label1"; 
			this.label1.Size = new System.Drawing.Size(72, 16); 
			this.label1.TabIndex = 5; 
			this.label1.Text = "Source"; 
			//  
			// button1 
			//  
			this.button1.Location = new System.Drawing.Point(408, 24); 
			this.button1.Name = "button1"; 
			this.button1.Size = new System.Drawing.Size(72, 24); 
			this.button1.TabIndex = 1; 
			this.button1.Text = "button1"; 
			this.button1.Click += new System.EventHandler(this.button1_Click); 
			//  
			// label2 
			//  
			this.label2.Location = new System.Drawing.Point(8, 48); 
			this.label2.Name = "label2"; 
			this.label2.Size = new System.Drawing.Size(104, 16); 
			this.label2.TabIndex = 6; 
			this.label2.Text = "Destination"; 
			//  
			// progressBar2 
			//  
			this.progressBar2.Location = new System.Drawing.Point(8, 192); 
			this.progressBar2.Name = "progressBar2"; 
			this.progressBar2.Size = new System.Drawing.Size(336, 16); 
			this.progressBar2.TabIndex = 7; 
			//  
			// progressBar3 
			//  
			this.progressBar3.Location = new System.Drawing.Point(8, 216); 
			this.progressBar3.Name = "progressBar3"; 
			this.progressBar3.Size = new System.Drawing.Size(336, 16); 
			this.progressBar3.TabIndex = 8; 
			//  
			// progressBar4 
			//  
			this.progressBar4.Location = new System.Drawing.Point(8, 240); 
			this.progressBar4.Name = "progressBar4"; 
			this.progressBar4.Size = new System.Drawing.Size(336, 16); 
			this.progressBar4.TabIndex = 9; 
			//  
			// label3 
			//  
			this.label3.Location = new System.Drawing.Point(352, 168); 
			this.label3.Name = "label3"; 
			this.label3.Size = new System.Drawing.Size(120, 16); 
			this.label3.TabIndex = 10; 
			this.label3.Text = "label3"; 
			//  
			// label4 
			//  
			this.label4.Location = new System.Drawing.Point(352, 192); 
			this.label4.Name = "label4"; 
			this.label4.Size = new System.Drawing.Size(120, 16); 
			this.label4.TabIndex = 11; 
			this.label4.Text = "label4"; 
			//  
			// label5 
			//  
			this.label5.Location = new System.Drawing.Point(352, 216); 
			this.label5.Name = "label5"; 
			this.label5.Size = new System.Drawing.Size(120, 16); 
			this.label5.TabIndex = 12; 
			this.label5.Text = "label5"; 
			//  
			// label6 
			//  
			this.label6.Location = new System.Drawing.Point(352, 240); 
			this.label6.Name = "label6"; 
			this.label6.Size = new System.Drawing.Size(120, 16); 
			this.label6.TabIndex = 13; 
			this.label6.Text = "label6"; 
			//  
			// Form1 
			//  
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13); 
			this.ClientSize = new System.Drawing.Size(488, 261); 
			this.Controls.AddRange(new System.Windows.Forms.Control[] { 
																		  this.label6, 
																		  this.label5, 
																		  this.label4, 
																		  this.label3, 
																		  this.progressBar4, 
																		  this.progressBar3, 
																		  this.progressBar2, 
																		  this.label2, 
																		  this.label1, 
																		  this.progressBar1, 
																		  this.textBox2, 
																		  this.button1, 
																		  this.textBox1}); 
			this.Name = "Form1"; 
			this.Text = "Form1"; 
			this.ResumeLayout(false); 
 
		} 
 
 
		///  
		/// The main entry point for the application. 
		///  
		[STAThread] 
		static void Main()  
		{ 
			Application.Run(new Form1()); 
		} 
 
		private void button1_Click(object sender, System.EventArgs e) 
		{ 
 
			// Check the protocol 
			string urlRequested = this.textBox1.Text; 
			switch (urlRequested.Substring(0, urlRequested.IndexOf(":"))) 
			{ 
				case "ftp": 
					// Set up the progress bar 
 
					// Create a new file download 
					fileDown = new FileDownload(); 
					fileDown.SourceAddress = urlRequested; 
					Debug.WriteLine("SourceAddress=" + fileDown.SourceAddress); 
					fileDown.DestinationAddress = this.textBox2.Text; 
					Debug.WriteLine("DestinationAddress=" + fileDown.DestinationAddress); 
 
					// Bind to events 
					fileDown.DownloadStartedEvent+=new FileDownload.DownloadStartedEventHandler(DownloadStarted); 
					fileDown.DataReceivedEvent+=new FileDownload.DataReceivedEventHandler(DataReceived); 
					fileDown.DownloadFinishedEvent+=new FileDownload.DownloadFinishedEventHandler(DownloadFinished); 
 
					// Set up progress references 
					progressBar = new ProgressBar[4] {progressBar1, progressBar2, progressBar3, progressBar4}; 
					progressLabel = new Label[4] {label3, label4, label5, label6}; 
 
					// Start the async download process 
					fileDown.AsyncDownload(); 
 
					// Synchronous download call with split and resume 
					//ftpClient1.Download(ftpURL.FileName, fileDown.DestinationAddress + ftpURL.FileName + ".1", true, 0, 1000); 
					//ftpClient1.Download(ftpURL.FileName, fileDown.DestinationAddress + ftpURL.FileName + ".1", true, 0, 2000); 
					//ftpClient1.Download(ftpURL.FileName, fileDown.DestinationAddress + ftpURL.FileName + ".3", true, 2000, 0); 
					//ftpClient1.Close(); 
					 
					break; 
				case "http": 
					break; 
				case "https": 
					break; 
				default: 
					break; 
			} 
		} 
 
		// Download started event handler 
		private void DownloadStarted(FileDownload.DownloadStartedEventArgs e) 
		{ 
			try 
			{ 
				Debug.WriteLine("Form1:DownloadStarted event raised"); 
				progressBar[e.PartNumber].Minimum = 0; 
				progressBar[e.PartNumber].Maximum = (int)fileDown[e.PartNumber].PartSize; 
				progressBar[e.PartNumber].Value = 0; 
				progressLabel[e.PartNumber].Text = "Download started"; 
			} 
			catch 
			{ 
			} 
		} 
 
		// Data received event handler 
		private void DataReceived(FileDownload.DataReceivedEventArgs e) 
		{ 
			try 
			{ 
				progressBar[e.PartNumber].Value = (int)e.BytesDownloaded; 
				progressLabel[e.PartNumber].Text = "Receiving data..."; 
			} 
			catch 
			{ 
				// Ignore errors 
			} 
		} 
 
		// Download finished callback 
		private void DownloadFinished(FileDownload.DownloadFinishedEventArgs e) 
		{ 
			try 
			{ 
				Debug.WriteLine("Form1:DownloadFinished event raised"); 
				progressLabel[e.PartNumber].Text = "Download complete."; 
			} 
			catch 
			{ 
			} 
		}  
	} 
}