Click here to Skip to main content
15,887,214 members
Articles / Programming Languages / C#

Console FTP in C#

Rate me:
Please Sign up or sign in to vote.
3.78/5 (17 votes)
7 Mar 20043 min read 224.5K   3K   46  
A basic FTP client in C#.
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.IO;
using Ftp;
using System.Runtime.InteropServices;
using Ftp.Interagent;


namespace JFtp
{
	/// <summary>
	/// Summary description for Form1.
	/// </summary>
	public class JFtp : System.Windows.Forms.Form
	{
		[DllImport("User32.dll")]
		private static extern bool SendMessage(IntPtr hwnd, UInt32 msg, UInt32 
			wParam, UInt32 lParam);

		private bool pasv, exiting;
		private JFtpHelper jFtpHelper;
		string ip;
		private int splitterCounter;
		private ExtFtp.ExtEventHandler ftpConn;
		private ExtFtpIa.ExtIaEventHandler jHandler;
		public delegate void AddToLogHandler();
		ListViewItem logItem;

		#region auto generated variables

		private System.Windows.Forms.MenuItem miConnect;
		private System.Windows.Forms.MenuItem miDisconnect;
		private System.Windows.Forms.MenuItem miExit;
		/// <summary>
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.Container components = null;

		#endregion		
		private System.Windows.Forms.TreeView lclTreeView;
		private System.Windows.Forms.TreeView rmtTreeView;
		private System.Windows.Forms.ListView lclListView;
		private System.Windows.Forms.ListView rmtListView;
		private System.Windows.Forms.ListView log;
		private System.Windows.Forms.Splitter logSplitter;
		private System.Windows.Forms.Panel lclPanel;
		private System.Windows.Forms.Panel rmtPanel;
		private System.Windows.Forms.Splitter lclSplitter;
		private System.Windows.Forms.Splitter rmtSplitter;
		private System.Windows.Forms.Splitter splitter1;
		private System.Windows.Forms.ColumnHeader columnHeader2;
		private System.Windows.Forms.ColumnHeader columnHeader3;
		private System.Windows.Forms.ColumnHeader columnHeader4;
		private System.Windows.Forms.ColumnHeader columnHeader5;
		private System.Windows.Forms.ColumnHeader columnHeader6;
		private System.Windows.Forms.ColumnHeader columnHeader7;
		private System.Windows.Forms.ColumnHeader columnHeader8;
		private System.Windows.Forms.ColumnHeader columnHeader9;
		private System.Windows.Forms.ColumnHeader columnHeader10;
		private System.Windows.Forms.Splitter panelsSplitter;
		private System.Windows.Forms.ColumnHeader logColumnHeader;
		private System.Windows.Forms.MainMenu mainMenu;
		private System.Windows.Forms.MenuItem fileMenu;
		private System.Windows.Forms.ContextMenu lclCtxtMnu;
		private System.Windows.Forms.ContextMenu rmtCtxtMnu;
		private System.Windows.Forms.MenuItem lclMiOpen;
		private System.Windows.Forms.MenuItem rmtMiOpen;
		private System.Windows.Forms.MenuItem lclMiUpload;
		private System.Windows.Forms.MenuItem lclMiDelete;
		private System.Windows.Forms.MenuItem lclMiNewDir;
		private System.Windows.Forms.MenuItem lclMiRen;
		private System.Windows.Forms.MenuItem lclMiRefresh;
		private System.Windows.Forms.MenuItem rmtMiDownload;
		private System.Windows.Forms.MenuItem rmtMiDel;
		private System.Windows.Forms.MenuItem rmtMiRen;
		private System.Windows.Forms.MenuItem rmtMiNewDir;
		private System.Windows.Forms.MenuItem rmtMiRefresh;
		private System.Windows.Forms.MenuItem rmtMiProps;
		private System.Windows.Forms.MenuItem lclMiProps;

		public JFtp()
		{
			//
			// Required for Windows Form Designer support
			//
			InitializeComponent();
			splitterCounter = 0;			
			//populate local tree view and list view
			DisableRemoteMenuItems();
			lclMiUpload.Enabled = false;
			exiting = false;
			new LocalHelper(lclTreeView, lclListView);
		}

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		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.mainMenu = new System.Windows.Forms.MainMenu();
			this.fileMenu = new System.Windows.Forms.MenuItem();
			this.miConnect = new System.Windows.Forms.MenuItem();
			this.miDisconnect = new System.Windows.Forms.MenuItem();
			this.miExit = new System.Windows.Forms.MenuItem();
			this.log = new System.Windows.Forms.ListView();
			this.logColumnHeader = new System.Windows.Forms.ColumnHeader();
			this.lclTreeView = new System.Windows.Forms.TreeView();
			this.rmtTreeView = new System.Windows.Forms.TreeView();
			this.lclListView = new System.Windows.Forms.ListView();
			this.columnHeader2 = new System.Windows.Forms.ColumnHeader();
			this.columnHeader3 = new System.Windows.Forms.ColumnHeader();
			this.columnHeader4 = new System.Windows.Forms.ColumnHeader();
			this.columnHeader5 = new System.Windows.Forms.ColumnHeader();
			this.lclCtxtMnu = new System.Windows.Forms.ContextMenu();
			this.lclMiOpen = new System.Windows.Forms.MenuItem();
			this.lclMiUpload = new System.Windows.Forms.MenuItem();
			this.lclMiDelete = new System.Windows.Forms.MenuItem();
			this.lclMiNewDir = new System.Windows.Forms.MenuItem();
			this.lclMiRen = new System.Windows.Forms.MenuItem();
			this.lclMiRefresh = new System.Windows.Forms.MenuItem();
			this.lclMiProps = new System.Windows.Forms.MenuItem();
			this.rmtListView = new System.Windows.Forms.ListView();
			this.columnHeader6 = new System.Windows.Forms.ColumnHeader();
			this.columnHeader7 = new System.Windows.Forms.ColumnHeader();
			this.columnHeader8 = new System.Windows.Forms.ColumnHeader();
			this.columnHeader9 = new System.Windows.Forms.ColumnHeader();
			this.columnHeader10 = new System.Windows.Forms.ColumnHeader();
			this.rmtCtxtMnu = new System.Windows.Forms.ContextMenu();
			this.rmtMiOpen = new System.Windows.Forms.MenuItem();
			this.rmtMiDownload = new System.Windows.Forms.MenuItem();
			this.rmtMiDel = new System.Windows.Forms.MenuItem();
			this.rmtMiRen = new System.Windows.Forms.MenuItem();
			this.rmtMiNewDir = new System.Windows.Forms.MenuItem();
			this.rmtMiRefresh = new System.Windows.Forms.MenuItem();
			this.rmtMiProps = new System.Windows.Forms.MenuItem();
			this.logSplitter = new System.Windows.Forms.Splitter();
			this.lclPanel = new System.Windows.Forms.Panel();
			this.lclSplitter = new System.Windows.Forms.Splitter();
			this.rmtPanel = new System.Windows.Forms.Panel();
			this.rmtSplitter = new System.Windows.Forms.Splitter();
			this.splitter1 = new System.Windows.Forms.Splitter();
			this.panelsSplitter = new System.Windows.Forms.Splitter();
			this.lclPanel.SuspendLayout();
			this.rmtPanel.SuspendLayout();
			this.SuspendLayout();
			// 
			// mainMenu
			// 
			this.mainMenu.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
																					 this.fileMenu});
			// 
			// fileMenu
			// 
			this.fileMenu.Index = 0;
			this.fileMenu.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
																					 this.miConnect,
																					 this.miDisconnect,
																					 this.miExit});
			this.fileMenu.Text = "File";
			// 
			// miConnect
			// 
			this.miConnect.Index = 0;
			this.miConnect.Text = "Connect";
			this.miConnect.Click += new System.EventHandler(this.Connect);
			// 
			// miDisconnect
			// 
			this.miDisconnect.Enabled = false;
			this.miDisconnect.Index = 1;
			this.miDisconnect.Text = "Disconnect";
			this.miDisconnect.Click += new System.EventHandler(this.Disconnect);
			// 
			// miExit
			// 
			this.miExit.Index = 2;
			this.miExit.Text = "Exit";
			this.miExit.Click += new System.EventHandler(this.miExit_Click);
			// 
			// log
			// 
			this.log.Alignment = System.Windows.Forms.ListViewAlignment.Left;
			this.log.BackColor = System.Drawing.Color.White;
			this.log.Columns.AddRange(new System.Windows.Forms.ColumnHeader[] {
																				  this.logColumnHeader});
			this.log.Dock = System.Windows.Forms.DockStyle.Bottom;
			this.log.HeaderStyle = System.Windows.Forms.ColumnHeaderStyle.None;
			this.log.Location = new System.Drawing.Point(0, 365);
			this.log.MultiSelect = false;
			this.log.Name = "log";
			this.log.Size = new System.Drawing.Size(816, 104);
			this.log.TabIndex = 0;
			this.log.View = System.Windows.Forms.View.Details;
			// 
			// logColumnHeader
			// 
			this.logColumnHeader.Text = "";
			this.logColumnHeader.Width = 640;
			// 
			// lclTreeView
			// 
			this.lclTreeView.Dock = System.Windows.Forms.DockStyle.Left;
			this.lclTreeView.ImageIndex = -1;
			this.lclTreeView.Name = "lclTreeView";
			this.lclTreeView.SelectedImageIndex = -1;
			this.lclTreeView.Size = new System.Drawing.Size(121, 168);
			this.lclTreeView.TabIndex = 0;
			// 
			// rmtTreeView
			// 
			this.rmtTreeView.Dock = System.Windows.Forms.DockStyle.Left;
			this.rmtTreeView.ImageIndex = -1;
			this.rmtTreeView.Name = "rmtTreeView";
			this.rmtTreeView.PathSeparator = "/";
			this.rmtTreeView.SelectedImageIndex = -1;
			this.rmtTreeView.Size = new System.Drawing.Size(121, 194);
			this.rmtTreeView.TabIndex = 0;
			// 
			// lclListView
			// 
			this.lclListView.Columns.AddRange(new System.Windows.Forms.ColumnHeader[] {
																						  this.columnHeader2,
																						  this.columnHeader3,
																						  this.columnHeader4,
																						  this.columnHeader5});
			this.lclListView.ContextMenu = this.lclCtxtMnu;
			this.lclListView.Dock = System.Windows.Forms.DockStyle.Fill;
			this.lclListView.FullRowSelect = true;
			this.lclListView.Location = new System.Drawing.Point(124, 0);
			this.lclListView.Name = "lclListView";
			this.lclListView.Size = new System.Drawing.Size(692, 168);
			this.lclListView.TabIndex = 2;
			this.lclListView.View = System.Windows.Forms.View.Details;
			// 
			// columnHeader2
			// 
			this.columnHeader2.Text = "Name";
			this.columnHeader2.Width = 281;
			// 
			// columnHeader3
			// 
			this.columnHeader3.Text = "Size";
			this.columnHeader3.TextAlign = System.Windows.Forms.HorizontalAlignment.Right;
			this.columnHeader3.Width = 112;
			// 
			// columnHeader4
			// 
			this.columnHeader4.Text = "Type";
			// 
			// columnHeader5
			// 
			this.columnHeader5.Text = "Modified";
			this.columnHeader5.Width = 126;
			// 
			// lclCtxtMnu
			// 
			this.lclCtxtMnu.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
																					   this.lclMiOpen,
																					   this.lclMiUpload,
																					   this.lclMiDelete,
																					   this.lclMiNewDir,
																					   this.lclMiRen,
																					   this.lclMiRefresh,
																					   this.lclMiProps});
			// 
			// lclMiOpen
			// 
			this.lclMiOpen.Index = 0;
			this.lclMiOpen.Text = "Open";
			// 
			// lclMiUpload
			// 
			this.lclMiUpload.Index = 1;
			this.lclMiUpload.Text = "Upload";
			this.lclMiUpload.Click += new System.EventHandler(this.UploadFiles);
			// 
			// lclMiDelete
			// 
			this.lclMiDelete.Index = 2;
			this.lclMiDelete.Text = "Delete";
			// 
			// lclMiNewDir
			// 
			this.lclMiNewDir.Index = 3;
			this.lclMiNewDir.Text = "New Folder";
			// 
			// lclMiRen
			// 
			this.lclMiRen.Index = 4;
			this.lclMiRen.Text = "Rename";
			// 
			// lclMiRefresh
			// 
			this.lclMiRefresh.Index = 5;
			this.lclMiRefresh.Text = "Refresh";
			// 
			// lclMiProps
			// 
			this.lclMiProps.Index = 6;
			this.lclMiProps.Text = "Properties";
			// 
			// rmtListView
			// 
			this.rmtListView.BackColor = System.Drawing.SystemColors.Window;
			this.rmtListView.Columns.AddRange(new System.Windows.Forms.ColumnHeader[] {
																						  this.columnHeader6,
																						  this.columnHeader7,
																						  this.columnHeader8,
																						  this.columnHeader9,
																						  this.columnHeader10});
			this.rmtListView.ContextMenu = this.rmtCtxtMnu;
			this.rmtListView.Dock = System.Windows.Forms.DockStyle.Fill;
			this.rmtListView.FullRowSelect = true;
			this.rmtListView.Location = new System.Drawing.Point(124, 0);
			this.rmtListView.Name = "rmtListView";
			this.rmtListView.Size = new System.Drawing.Size(692, 194);
			this.rmtListView.TabIndex = 2;
			this.rmtListView.View = System.Windows.Forms.View.Details;
			// 
			// columnHeader6
			// 
			this.columnHeader6.Text = "Name";
			this.columnHeader6.Width = 277;
			// 
			// columnHeader7
			// 
			this.columnHeader7.Text = "Size";
			this.columnHeader7.TextAlign = System.Windows.Forms.HorizontalAlignment.Right;
			this.columnHeader7.Width = 106;
			// 
			// columnHeader8
			// 
			this.columnHeader8.Text = "Type";
			// 
			// columnHeader9
			// 
			this.columnHeader9.Text = "Modified";
			this.columnHeader9.Width = 132;
			// 
			// columnHeader10
			// 
			this.columnHeader10.Text = "Permissions";
			this.columnHeader10.Width = 94;
			// 
			// rmtCtxtMnu
			// 
			this.rmtCtxtMnu.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
																					   this.rmtMiOpen,
																					   this.rmtMiDownload,
																					   this.rmtMiDel,
																					   this.rmtMiRen,
																					   this.rmtMiNewDir,
																					   this.rmtMiRefresh,
																					   this.rmtMiProps});
			// 
			// rmtMiOpen
			// 
			this.rmtMiOpen.Index = 0;
			this.rmtMiOpen.Text = "Open";
			// 
			// rmtMiDownload
			// 
			this.rmtMiDownload.Index = 1;
			this.rmtMiDownload.Text = "Download";
			this.rmtMiDownload.Click += new System.EventHandler(this.DownloadFiles);
			// 
			// rmtMiDel
			// 
			this.rmtMiDel.Index = 2;
			this.rmtMiDel.Text = "Delete";
			// 
			// rmtMiRen
			// 
			this.rmtMiRen.Index = 3;
			this.rmtMiRen.Text = "Rename";
			// 
			// rmtMiNewDir
			// 
			this.rmtMiNewDir.Index = 4;
			this.rmtMiNewDir.Text = "New Folder";
			// 
			// rmtMiRefresh
			// 
			this.rmtMiRefresh.Index = 5;
			this.rmtMiRefresh.Text = "Refresh";
			// 
			// rmtMiProps
			// 
			this.rmtMiProps.Index = 6;
			this.rmtMiProps.Text = "Properties";
			// 
			// logSplitter
			// 
			this.logSplitter.Dock = System.Windows.Forms.DockStyle.Bottom;
			this.logSplitter.Location = new System.Drawing.Point(0, 362);
			this.logSplitter.Name = "logSplitter";
			this.logSplitter.Size = new System.Drawing.Size(816, 3);
			this.logSplitter.TabIndex = 1;
			this.logSplitter.TabStop = false;
			// 
			// lclPanel
			// 
			this.lclPanel.Controls.AddRange(new System.Windows.Forms.Control[] {
																				   this.lclListView,
																				   this.lclSplitter,
																				   this.lclTreeView});
			this.lclPanel.Dock = System.Windows.Forms.DockStyle.Bottom;
			this.lclPanel.Location = new System.Drawing.Point(0, 194);
			this.lclPanel.Name = "lclPanel";
			this.lclPanel.Size = new System.Drawing.Size(816, 168);
			this.lclPanel.TabIndex = 2;
			// 
			// lclSplitter
			// 
			this.lclSplitter.Location = new System.Drawing.Point(121, 0);
			this.lclSplitter.Name = "lclSplitter";
			this.lclSplitter.Size = new System.Drawing.Size(3, 168);
			this.lclSplitter.TabIndex = 1;
			this.lclSplitter.TabStop = false;
			this.lclSplitter.SplitterMoved += new System.Windows.Forms.SplitterEventHandler(this.UpdateSplitters);
			// 
			// rmtPanel
			// 
			this.rmtPanel.Controls.AddRange(new System.Windows.Forms.Control[] {
																				   this.rmtListView,
																				   this.rmtSplitter,
																				   this.rmtTreeView});
			this.rmtPanel.Dock = System.Windows.Forms.DockStyle.Fill;
			this.rmtPanel.Name = "rmtPanel";
			this.rmtPanel.Size = new System.Drawing.Size(816, 194);
			this.rmtPanel.TabIndex = 3;
			// 
			// rmtSplitter
			// 
			this.rmtSplitter.Location = new System.Drawing.Point(121, 0);
			this.rmtSplitter.Name = "rmtSplitter";
			this.rmtSplitter.Size = new System.Drawing.Size(3, 194);
			this.rmtSplitter.TabIndex = 1;
			this.rmtSplitter.TabStop = false;
			this.rmtSplitter.SplitterMoved += new System.Windows.Forms.SplitterEventHandler(this.UpdateSplitters);
			// 
			// splitter1
			// 
			this.splitter1.Name = "splitter1";
			this.splitter1.TabIndex = 0;
			this.splitter1.TabStop = false;
			// 
			// panelsSplitter
			// 
			this.panelsSplitter.Dock = System.Windows.Forms.DockStyle.Bottom;
			this.panelsSplitter.Location = new System.Drawing.Point(0, 191);
			this.panelsSplitter.Name = "panelsSplitter";
			this.panelsSplitter.Size = new System.Drawing.Size(816, 3);
			this.panelsSplitter.TabIndex = 5;
			this.panelsSplitter.TabStop = false;
			// 
			// JFtp
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.ClientSize = new System.Drawing.Size(816, 469);
			this.Controls.AddRange(new System.Windows.Forms.Control[] {
																		  this.panelsSplitter,
																		  this.rmtPanel,
																		  this.lclPanel,
																		  this.logSplitter,
																		  this.log});
			this.Menu = this.mainMenu;
			this.Name = "JFtp";
			this.Text = "jFTP";
			this.WindowState = System.Windows.Forms.FormWindowState.Maximized;
			this.Resize += new System.EventHandler(this.ReadjustSize);
			this.Load += new System.EventHandler(this.AdjustWindow);
			this.lclPanel.ResumeLayout(false);
			this.rmtPanel.ResumeLayout(false);
			this.ResumeLayout(false);

		}
		#endregion

		/// <summary>
		/// The main entry point for the application.
		/// </summary>
		[STAThread]
		static void Main() 
		{
			Application.Run(new JFtp());
		}

		/// <summary>
		/// Keeps the splitters vertically equal
		/// </summary>
		/// <param name="sender">the splitter that raised the event</param>
		/// <param name="e">the event arguments</param>
		private void UpdateSplitters(object sender, System.Windows.Forms.SplitterEventArgs e)
		{
			if(sender == rmtSplitter)
			{
				if(splitterCounter <= 1)
				{
					++splitterCounter;
					lclSplitter.SplitPosition = rmtSplitter.SplitPosition;
				}
				else splitterCounter = 0;
				return;
			}
			else if(sender == lclSplitter)
			{
				if(splitterCounter <= 1)
				{
					++splitterCounter;
					rmtSplitter.SplitPosition = lclSplitter.SplitPosition;
				}
				else splitterCounter = 0;
				return;
			}
		}

		private void CreateHelper()
		{
			ftpConn = new ExtFtp.ExtEventHandler();
			jHandler = new ExtFtpIa.ExtIaEventHandler();
			jHandler.OnEndConnect += new CoreFtpIa.CoreIaEventHandler.EndConnectHandler(EndConnect);
			jHandler.OnDisconnected += new CoreFtpIa.CoreIaEventHandler.DisconnectedHandler(Disconnected);
			ftpConn.OnOutputClientCmd += new CoreFtp.FtpEventHandler.OutputMessageHandler(AddClassMsg);
			ftpConn.OnOutputMessage += new CoreFtp.FtpEventHandler.OutputMessageHandler(AddFtpCmd);
			ftpConn.OnOutputServerReply += new CoreFtp.FtpEventHandler.OutputMessageHandler(AddServerRply);
			jFtpHelper = new JFtpHelper(rmtTreeView, rmtListView, jHandler, ftpConn);
		}

		/// <summary>
		/// Adjusts the window panel sizes on load
		/// </summary>
		/// <param name="sender">the object that raised the event</param>
		/// <param name="e">the event arguments</param>
		private void AdjustWindow(object sender, System.EventArgs e)
		{			
			//int width = Width;
			int logHeight = (int) (Height * .2);
			int panelHeight = (int) (Height * .4);
			lclPanel.Height = panelHeight;
			rmtPanel.Height = panelHeight;
			log.Height = logHeight;
			logColumnHeader.Width = Width - 40;
			lclSplitter.SplitPosition = (int) (Width * .20);			
			return;
		}

		private void Disconnect(object sender, System.EventArgs e)
		{
			miDisconnect.Enabled = false;
			DisableRemoteMenuItems();
			//CleanUp();
			jFtpHelper.FtpCommandBuilder(FtpCmd.QUIT, null);			
		}

		private void Disconnected()
		{			
			DisableRemoteMenuItems();
			miDisconnect.Enabled = false;
			miConnect.Enabled = true;
			if(exiting) Application.Exit();
			//jFtpHelper = null;
			//ftpConn = null;
		}

		/// <summary>
		/// enables remote menu items such as open, delete, download, etc
		/// </summary>
		private void EnableRemoteMenuItems()
		{
			rmtMiDel.Enabled = true;
			rmtMiDownload.Enabled = true;
			rmtMiNewDir.Enabled = true;
			rmtMiOpen.Enabled = true;
			rmtMiProps.Enabled = true;
			rmtMiRefresh.Enabled = true;
			rmtMiRen.Enabled = true;
			lclMiUpload.Enabled = true;
		}

		/// <summary>
		/// disables remote menu items such as new folder, download, rename, etc
		/// </summary>
		private void DisableRemoteMenuItems()
		{
			rmtMiDel.Enabled = false;
			rmtMiDownload.Enabled = false;
			rmtMiNewDir.Enabled = false;
			rmtMiOpen.Enabled = false;
			rmtMiProps.Enabled = false;
			rmtMiRefresh.Enabled = false;
			rmtMiRen.Enabled = false;
			lclMiUpload.Enabled = false;
		}

		/// <summary>
		/// Converts a filesize to a string
		/// </summary>
		/// <param name="size"></param>
		/// <returns></returns>
		public static string GenerateFileSize(long size)
		{
			System.Globalization.NumberFormatInfo numFormat = new System.Globalization.NumberFormatInfo();
			numFormat.NumberDecimalDigits = 0;
			//between 0 and 10,000 bytes
			if(size < 10000) return size.ToString("n", numFormat);
			//is less than 10,000,000
			if(size < 10000000) 
			{
				size = size/1000;
				return size.ToString("n", numFormat) + " KB";
			}
			//is less than 1,000,000,000
			if(size < 1000000000)
			{
				size = size/1000000;
				return size.ToString("n", numFormat) + " MB";
			}
			//is more 1,000,000,000
			size = size/1000000000;
			return size.ToString("n", numFormat) + " GB";
		}	

		/// <summary>
		/// Connects to a FTP server
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void Connect(object sender, System.EventArgs e)
		{
			log.Items.Clear();
			ConnectWindow connWindow = new ConnectWindow();
			DialogResult ans = connWindow.ShowDialog();
			log.Text = "";
			CreateHelper();
			miConnect.Enabled = false;
			//user clicked ok
			if(ans == DialogResult.OK)
			{
				miConnect.Enabled = false;
				ip = connWindow.ip.Text;
				string un = connWindow.userName.Text;
				string pwd = connWindow.pwd.Text;
				string dir = connWindow.dDir.Text;
				int port;
				try
				{
					port = int.Parse(connWindow.port.Text);
				}
				catch(Exception)
				{
					miConnect.Enabled = true;
					return;
				}
				pasv = connWindow.pasv.Checked;
				log.Text = "";
				CreateHelper();
				jFtpHelper.Connect(ip, port, un, pwd, connWindow.pasv.Checked, dir);
			}
			else miConnect.Enabled = true;
		}

		/// <summary>
		/// Connected to server
		/// </summary>
		private void EndConnect(bool success)
		{
			//to get list
			if(success)
			{
				EnableRemoteMenuItems();
			}
			else
			{
				miDisconnect.Enabled = false;				
				miConnect.Enabled = true;
			}
			return;
		}

		
		private void UploadFiles(object sender, System.EventArgs e)
		{
			if((sender == lclMiUpload) && (lclListView.SelectedItems.Count > 0))
			{
				ListView.SelectedListViewItemCollection items = lclListView.SelectedItems;
				string path = lclTreeView.SelectedNode.FullPath;
				string[] files = new string[items.Count];
				for(int i = 0; i < items.Count; i++)
					files[i] = path + "\\" + items[i].Text;				
				jFtpHelper.SendFiles(files);
				return;
			}
		}

		private void DownloadFiles(object sender, System.EventArgs e)
		{
			if((sender == rmtMiDownload) && (rmtListView.SelectedItems.Count > 0))
			{
				ListView.SelectedListViewItemCollection items = rmtListView.SelectedItems;
				ListViewItem item = items[0];
				string path = jFtpHelper.GetSelectedDirectory();
				if(path == "/") path+=item.Text;
				else path = path + "/" + item.Text;
				string cwd = lclTreeView.SelectedNode.FullPath;
				if(!cwd.EndsWith("\\")) cwd+="\\";
				string[] files = new String[] {path};
				jFtpHelper.GetFiles(files, cwd);
				return;
			}
		}


		private void ReadjustSize(object sender, System.EventArgs e)
		{
			int logHeight = (int) (Height * .2);
			int panelHeight = (int) (Height * .4);
			lclPanel.Height = panelHeight;
			rmtPanel.Height = panelHeight;
			log.Height = logHeight;
			logColumnHeader.Width = Width - 40;
			lclSplitter.SplitPosition = (int) (Width * .20);
		}

		/// <summary>
		/// prepares the class for a disconnect
		/// </summary>
		private void CleanUp()
		{
			DisableRemoteMenuItems();
			lclMiUpload.Enabled = false;
			miDisconnect.Enabled = false;
		}

		private void ScrollLogToBottom()
		{			
			const System.Int32 WM_VSCROLL = 0x115;
			const System.Int32 SB_BOTTOM = 0x7;
			SendMessage(log.Handle, WM_VSCROLL, SB_BOTTOM , 0);
		}

		//Blue
		/// <summary>
		/// Receives commands sent by the program
		/// </summary>
		/// <param name="text"></param>
		private void AddFtpCmd(string text)
		{
			ListViewItem item = new ListViewItem(text);
			item.ForeColor = System.Drawing.Color.Blue;			
			logItem = item;
			log.Invoke(new AddToLogHandler(AddLogItem));
			return;
		}

		private void AddLogItem()
		{
			log.Items.Add(logItem);
			ScrollLogToBottom();
			return;
		}

		//DarkGreen
		/// <summary>
		/// receives FTP commands gegenrated by the client
		/// </summary>
		/// <param name="text"></param>
		private void AddClassMsg(string text)
		{
			ListViewItem item = new ListViewItem(text);
			item.ForeColor = System.Drawing.Color.DarkGreen;
			logItem = item;
			log.Invoke(new AddToLogHandler(AddLogItem));
			return;
		}

		//black
		/// <summary>
		/// receieves messages sent from the server
		/// </summary>
		/// <param name="text"></param>
		private void AddServerRply(string text)
		{
			ListViewItem item = new ListViewItem(text);
			if(text.StartsWith("550")) item.ForeColor = System.Drawing.Color.DarkRed;
			else item.ForeColor = System.Drawing.Color.Black;
			logItem = item;
			log.Invoke(new AddToLogHandler(AddLogItem));
			return;
		}

		private void miExit_Click(object sender, System.EventArgs e)
		{
			miExit.Enabled = false;
			if(miConnect.Enabled)
				Application.Exit();
			exiting = true;
			//begin clean up of stuff
			if(miDisconnect.Enabled)
				if(jFtpHelper.JFtpHelperStatus == JFtpHelper.JFtpHelperStatusModes.Ready)
					jFtpHelper.FtpCommandBuilder(FtpCmd.QUIT, null);
			return;
		}

	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions