www.pudn.com > SharpDownload(FTP¼°WEBÏÂÔØ).zip > FileDownloadMonitor.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 formFileDownload : System.Windows.Forms.Form 
	{ 
		private System.Windows.Forms.Label label2; 
		private System.Windows.Forms.Label label1; 
		///  
		/// Required designer variable. 
		///  
		private System.ComponentModel.Container components = null; 
 
		private FileDownload fileDown; 
		private DownloadStatus[] downloadStatus; 
 
		private const long MIN_SPLIT_SIZE = 20000; 
		private System.Windows.Forms.FolderBrowserDialog folderBrowserDialog1; 
		private System.Windows.Forms.Button buttonDownloadFolder; 
		private System.Windows.Forms.TextBox textDestination; 
		private System.Windows.Forms.Button buttonDownload; 
		private SharpDownload.DownloadStatus downloadStatus1; 
		private SharpDownload.DownloadStatus downloadStatus2; 
		private SharpDownload.DownloadStatus downloadStatus3; 
		private SharpDownload.DownloadStatus downloadStatus4; 
		private System.Windows.Forms.ComboBox comboSource; 
		private System.Windows.Forms.Label label3; 
		private System.Windows.Forms.Label label4; 
		private System.Windows.Forms.TextBox textUserID; 
		private System.Windows.Forms.TextBox textPassword; 
		private const int MAX_FILE_PARTS = 4; 
 
		///  
		/// Public constructor 
		///  
		public formFileDownload() 
		{ 
			// 
			// 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.textDestination = new System.Windows.Forms.TextBox(); 
			this.label1 = new System.Windows.Forms.Label(); 
			this.buttonDownload = new System.Windows.Forms.Button(); 
			this.label2 = new System.Windows.Forms.Label(); 
			this.folderBrowserDialog1 = new System.Windows.Forms.FolderBrowserDialog(); 
			this.buttonDownloadFolder = new System.Windows.Forms.Button(); 
			this.downloadStatus1 = new SharpDownload.DownloadStatus(); 
			this.downloadStatus2 = new SharpDownload.DownloadStatus(); 
			this.downloadStatus3 = new SharpDownload.DownloadStatus(); 
			this.downloadStatus4 = new SharpDownload.DownloadStatus(); 
			this.comboSource = new System.Windows.Forms.ComboBox(); 
			this.textUserID = new System.Windows.Forms.TextBox(); 
			this.label3 = new System.Windows.Forms.Label(); 
			this.label4 = new System.Windows.Forms.Label(); 
			this.textPassword = new System.Windows.Forms.TextBox(); 
			this.SuspendLayout(); 
			//  
			// textDestination 
			//  
			this.textDestination.Location = new System.Drawing.Point(8, 64); 
			this.textDestination.Name = "textDestination"; 
			this.textDestination.Size = new System.Drawing.Size(480, 20); 
			this.textDestination.TabIndex = 1; 
			this.textDestination.Text = "c:\\"; 
			//  
			// label1 
			//  
			this.label1.AutoSize = true; 
			this.label1.Location = new System.Drawing.Point(8, 8); 
			this.label1.Name = "label1"; 
			this.label1.Size = new System.Drawing.Size(120, 16); 
			this.label1.TabIndex = 5; 
			this.label1.Text = "URL to download from:"; 
			//  
			// buttonDownload 
			//  
			this.buttonDownload.Location = new System.Drawing.Point(496, 24); 
			this.buttonDownload.Name = "buttonDownload"; 
			this.buttonDownload.Size = new System.Drawing.Size(72, 24); 
			this.buttonDownload.TabIndex = 1; 
			this.buttonDownload.Text = "Download"; 
			this.buttonDownload.Click += new System.EventHandler(this.buttonDownload_Click); 
			//  
			// label2 
			//  
			this.label2.AutoSize = true; 
			this.label2.Location = new System.Drawing.Point(8, 48); 
			this.label2.Name = "label2"; 
			this.label2.Size = new System.Drawing.Size(130, 16); 
			this.label2.TabIndex = 6; 
			this.label2.Text = "Folder to save download:"; 
			//  
			// folderBrowserDialog1 
			//  
			this.folderBrowserDialog1.Description = "Select a folder for your download"; 
			//  
			// buttonDownloadFolder 
			//  
			this.buttonDownloadFolder.Location = new System.Drawing.Point(496, 64); 
			this.buttonDownloadFolder.Name = "buttonDownloadFolder"; 
			this.buttonDownloadFolder.Size = new System.Drawing.Size(24, 24); 
			this.buttonDownloadFolder.TabIndex = 14; 
			this.buttonDownloadFolder.Text = "..."; 
			this.buttonDownloadFolder.Click += new System.EventHandler(this.buttonDownloadFolder_Click); 
			//  
			// downloadStatus1 
			//  
			this.downloadStatus1.BytesDownloaded = ((long)(0)); 
			this.downloadStatus1.Location = new System.Drawing.Point(8, 144); 
			this.downloadStatus1.Name = "downloadStatus1"; 
			this.downloadStatus1.PartNumber = 0; 
			this.downloadStatus1.PartSize = ((long)(0)); 
			this.downloadStatus1.Size = new System.Drawing.Size(552, 16); 
			this.downloadStatus1.Status = ""; 
			this.downloadStatus1.TabIndex = 16; 
			//  
			// downloadStatus2 
			//  
			this.downloadStatus2.BytesDownloaded = ((long)(0)); 
			this.downloadStatus2.Location = new System.Drawing.Point(8, 168); 
			this.downloadStatus2.Name = "downloadStatus2"; 
			this.downloadStatus2.PartNumber = 0; 
			this.downloadStatus2.PartSize = ((long)(0)); 
			this.downloadStatus2.Size = new System.Drawing.Size(552, 16); 
			this.downloadStatus2.Status = ""; 
			this.downloadStatus2.TabIndex = 17; 
			//  
			// downloadStatus3 
			//  
			this.downloadStatus3.BytesDownloaded = ((long)(0)); 
			this.downloadStatus3.Location = new System.Drawing.Point(8, 192); 
			this.downloadStatus3.Name = "downloadStatus3"; 
			this.downloadStatus3.PartNumber = 0; 
			this.downloadStatus3.PartSize = ((long)(0)); 
			this.downloadStatus3.Size = new System.Drawing.Size(552, 16); 
			this.downloadStatus3.Status = ""; 
			this.downloadStatus3.TabIndex = 18; 
			//  
			// downloadStatus4 
			//  
			this.downloadStatus4.BytesDownloaded = ((long)(0)); 
			this.downloadStatus4.Location = new System.Drawing.Point(8, 216); 
			this.downloadStatus4.Name = "downloadStatus4"; 
			this.downloadStatus4.PartNumber = 0; 
			this.downloadStatus4.PartSize = ((long)(0)); 
			this.downloadStatus4.Size = new System.Drawing.Size(552, 16); 
			this.downloadStatus4.Status = ""; 
			this.downloadStatus4.TabIndex = 19; 
			//  
			// comboSource 
			//  
			this.comboSource.Items.AddRange(new object[] { 
															 "ftp://ftp.cdrom.com/pub/simtelnet/winxp/arcade/simon32.zip", 
															 "http://chefax.fe.up.pt/putty/latest/x86/pageant.exe"}); 
			this.comboSource.Location = new System.Drawing.Point(8, 24); 
			this.comboSource.Name = "comboSource"; 
			this.comboSource.Size = new System.Drawing.Size(480, 21); 
			this.comboSource.TabIndex = 0; 
			//  
			// textUserID 
			//  
			this.textUserID.Location = new System.Drawing.Point(8, 104); 
			this.textUserID.Name = "textUserID"; 
			this.textUserID.Size = new System.Drawing.Size(200, 20); 
			this.textUserID.TabIndex = 2; 
			this.textUserID.Text = ""; 
			//  
			// label3 
			//  
			this.label3.AutoSize = true; 
			this.label3.Location = new System.Drawing.Point(8, 88); 
			this.label3.Name = "label3"; 
			this.label3.Size = new System.Drawing.Size(46, 16); 
			this.label3.TabIndex = 22; 
			this.label3.Text = "User ID:"; 
			//  
			// label4 
			//  
			this.label4.AutoSize = true; 
			this.label4.Location = new System.Drawing.Point(288, 88); 
			this.label4.Name = "label4"; 
			this.label4.Size = new System.Drawing.Size(57, 16); 
			this.label4.TabIndex = 24; 
			this.label4.Text = "Password:"; 
			//  
			// textPassword 
			//  
			this.textPassword.Location = new System.Drawing.Point(288, 104); 
			this.textPassword.Name = "textPassword"; 
			this.textPassword.Size = new System.Drawing.Size(200, 20); 
			this.textPassword.TabIndex = 3; 
			this.textPassword.Text = ""; 
			//  
			// formFileDownload 
			//  
			this.AcceptButton = this.buttonDownload; 
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13); 
			this.ClientSize = new System.Drawing.Size(578, 239); 
			this.Controls.Add(this.label4); 
			this.Controls.Add(this.textPassword); 
			this.Controls.Add(this.label3); 
			this.Controls.Add(this.textUserID); 
			this.Controls.Add(this.textDestination); 
			this.Controls.Add(this.label2); 
			this.Controls.Add(this.label1); 
			this.Controls.Add(this.comboSource); 
			this.Controls.Add(this.downloadStatus4); 
			this.Controls.Add(this.downloadStatus3); 
			this.Controls.Add(this.downloadStatus2); 
			this.Controls.Add(this.downloadStatus1); 
			this.Controls.Add(this.buttonDownloadFolder); 
			this.Controls.Add(this.buttonDownload); 
			this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle; 
			this.MaximizeBox = false; 
			this.Name = "formFileDownload"; 
			this.Text = "File Download"; 
			this.Load += new System.EventHandler(this.formFileDownload_Load); 
			this.ResumeLayout(false); 
 
		} 
 
		private void buttonDownload_Click(object sender, System.EventArgs e) 
		{ 
 
			// Check the protocol 
			string urlRequested = this.comboSource.Text; 
			switch (urlRequested.Substring(0, urlRequested.IndexOf(":"))) 
			{ 
				case "ftp": 
					// Disable download button for now... 
					buttonDownload.Enabled = false; 
 
					// Create a new file download 
					fileDown = new FileDownload(); 
					fileDown.SourceAddress = urlRequested; 
					Debug.WriteLine("SourceAddress=" + fileDown.SourceAddress); 
					fileDown.LocalFolder = this.textDestination.Text; 
					Debug.WriteLine("Downloadfolder=" + fileDown.LocalFolder); 
 
					// Check if user id and password set 
					if (textUserID.Text.Length!=0 | textPassword.Text.Length!=0) 
					{ 
						fileDown.DownloadInfo.UserName = textUserID.Text; 
						fileDown.DownloadInfo.Password = textPassword.Text; 
					} 
 
					// Bind to events 
					fileDown.DownloadStartedEvent+=new FileDownload.DownloadStartedEventHandler(DownloadStarted); 
					fileDown.DataReceivedEvent+=new FileDownload.DataReceivedEventHandler(DataReceived); 
					fileDown.DownloadFinishedEvent+=new FileDownload.DownloadFinishedEventHandler(DownloadFinished); 
					fileDown.DownloadCompletedEvent+=new FileDownload.DownloadCompletedEventHandler(DownloadCompleted); 
					fileDown.DownloadFailedEvent+=new FileDownload.DownloadFailedEventHandler(DownloadFailed); 
 
					// Set up progress references 
					downloadStatus = new DownloadStatus[4] {downloadStatus1, downloadStatus2, downloadStatus3, downloadStatus4}; 
					for (int i=0;i 
		/// Download started event handler 
		///  
		///  
		private void DownloadStarted(FileDownload.DownloadStartedEventArgs e) 
		{ 
			try 
			{ 
				Debug.WriteLine("Form1:DownloadStarted event raised"); 
 
				// Update download status display 
				downloadStatus[e.PartNumber].Status = "Download started"; 
				downloadStatus[e.PartNumber].PartSize = fileDown[e.PartNumber].PartSize; 
				downloadStatus[e.PartNumber].UpdateDisplay(); 
			} 
			catch (Exception eStarted) 
			{ 
				// Log errors 
				Debug.WriteLine("FileDownloadMonitor: Error occurred in DataStarted event handler ["+ eStarted.Message +"]"); 
			} 
		} 
 
		///  
		/// Data received event handler 
		///  
		/// data received event args 
		private void DataReceived(FileDownload.DataReceivedEventArgs e) 
		{ 
			try 
			{ 
				// Update the part status display 
				downloadStatus[e.PartNumber].BytesDownloaded = e.BytesDownloaded; 
				downloadStatus[e.PartNumber].Status = "Receiving data..."; 
				downloadStatus[e.PartNumber].UpdateDisplay(); 
			} 
			catch (Exception eReceived) 
			{ 
				// Log errors 
				Debug.WriteLine("FileDownloadMonitor: Error occurred in DataReceived event handler ["+ eReceived.Message +"]"); 
			} 
		} 
 
		///  
		/// Data failed event handler 
		///  
		///  
		private void DownloadFailed(FileDownload.DownloadFailedEventArgs e) 
		{ 
			try 
			{ 
				MessageBox.Show(this, "Download failed! Reason: " + e.Message); 
			} 
			catch (Exception eFailed) 
			{ 
				// Log errors 
				Debug.WriteLine("FileDownloadMonitor: Error occurred in DownloadFailed event handler ["+ eFailed.Message +"]"); 
			} 
		} 
 
		///  
		/// Download finished callback 
		///  
		///  
		private void DownloadFinished(FileDownload.DownloadFinishedEventArgs e) 
		{ 
			try 
			{ 
				Debug.WriteLine("Form1:DownloadFinished event raised"); 
 
				// Set download status 
				downloadStatus[e.PartNumber].Status = "Download complete"; 
				downloadStatus[e.PartNumber].UpdateDisplay(); 
			} 
			catch (Exception eFinished) 
			{ 
				// Log errors 
				Debug.WriteLine("FileDownloadMonitor: Error occurred in DownloadFinished event handler ["+ eFinished.Message +"]"); 
			} 
		} 
 
		///  
		/// Download completed event handler 
		///  
		///  
		private void DownloadCompleted(FileDownload.DownloadCompletedEventArgs e) 
		{ 
			try 
			{ 
				Debug.WriteLine("Form1: DownloadCompleted event raised"); 
				MessageBox.Show("Download completed!!!"); 
			} 
			catch (Exception eCompleted) 
			{ 
				// Log errors 
				Debug.WriteLine("FileDownloadMonitor: Error occurred in DownloadCompleted event handler ["+ eCompleted.Message +"]"); 
			} 
		} 
 
		#endregion 
 
		private void formFileDownload_Load(object sender, System.EventArgs e) 
		{ 
			// Auto-select first item 
			if (this.comboSource.Items.Count>0) 
			{ 
				this.comboSource.SelectedIndex = 0; 
			} 
		} 
 
	} 
}