Click here to Skip to main content
15,898,222 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.9K   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;

namespace JFtp
{
	/// <summary>
	/// Summary description for LocalHelper.
	/// </summary>
	public class LocalHelper
	{
		public LocalHelper(TreeView lclTreeView, ListView lclListView)
		{
			this.lclTreeView = lclTreeView;
			this.lclListView = lclListView;
			
			this.lclListView.DoubleClick += new System.EventHandler(UpdateLclListView);
			this.lclTreeView.AfterSelect += new System.Windows.Forms.TreeViewEventHandler(UpdateLclTreeView);
			InitializeLclTreeView();
		}

		
		/// <summary>
		/// populates the local tree view with data
		/// </summary>
		private void InitializeLclTreeView()
		{
			string[] drives = Directory.GetLogicalDrives();
			TreeNode[] nodes = new TreeNode[drives.Length];
			string rootDrive = Path.GetPathRoot(System.Environment.SystemDirectory);
			int rootIndex  = 0;
			for(int i = 0; i < drives.Length; i++)
			{
				if(drives[i] == rootDrive) rootIndex = i;
				drives[i] = drives[i].Replace("\\", "");
				nodes[i] = new TreeNode(drives[i]);
			}
			lclTreeView.Nodes.AddRange(nodes);
			lclTreeView.SelectedNode = nodes[rootIndex];
			return;
		}

		/// <summary>
		/// populates the local list view with data
		/// </summary>
		/// <param name="path">the directory to populate the data with</param>
		private void UpdateLclListView(string path)
		{			
			lclListView.Items.Clear();
			if(!path.EndsWith("\\")) path = path+="\\";
			string rootDrive = path;
			string[] files, dirs;
			//incase a floppy drive is selected or something
			try
			{
				files = Directory.GetFiles(rootDrive);
				dirs = Directory.GetDirectories(rootDrive);
			}
			catch(Exception)
			{
				files = new string[0];
				dirs = new string[0];
			}

			lclTreeView.SelectedNode.Nodes.Clear();
			//add directories
			foreach(string dir in dirs)
			{
				DirectoryInfo currDir = new DirectoryInfo(dir);
				bool hidden = false;
				if((FileAttributes.Hidden & currDir.Attributes) == FileAttributes.Hidden)
					hidden = true;
				if(!hidden)
				{
					ListViewItem item = new ListViewItem(currDir.Name);
					lclTreeView.SelectedNode.Nodes.Add(currDir.Name);
					item.SubItems.Add("");
					item.SubItems.Add("DIR");
					item.SubItems.Add(currDir.CreationTime.ToString("g"));
					lclListView.Items.Add(item);
				}
			}
			foreach(string file in files)
			{
				FileInfo currFile = new FileInfo(file);
				bool hidden = false;
				if((FileAttributes.Hidden & currFile.Attributes) == FileAttributes.Hidden)
					hidden = true;
				if(!hidden)
				{
					ListViewItem item = new ListViewItem(currFile.Name);
					item.SubItems.Add(JFtp.GenerateFileSize(currFile.Length));
					//item.SubItems.Add("FILE");
					item.SubItems.Add(Path.GetExtension(currFile.Name));
					item.SubItems.Add(currFile.CreationTime.ToString("g"));
					lclListView.Items.Add(item);
				}
			}
			return;
		}
		
		/// <summary>
		/// populates the treeview with folders
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void UpdateLclTreeView(object sender, System.Windows.Forms.TreeViewEventArgs e)
		{
			if(sender == lclTreeView)
			{
				UpdateLclListView(lclTreeView.SelectedNode.FullPath);
				lclTreeView.SelectedNode.Expand();
				return;
			}
		}	

		
		private void UpdateLclListView(object sender, System.EventArgs e)
		{
			if(sender == lclListView)
			{
				ListView.SelectedListViewItemCollection items = lclListView.SelectedItems;
				ListViewItem item = items[0];
				foreach(TreeNode node in lclTreeView.SelectedNode.Nodes)
				{
					if(node.Text == item.Text)
					{
						lclTreeView.SelectedNode = node;
						lclTreeView.SelectedNode.Expand();
						return;
					}
				}
				return;
			}		
		}

		private TreeView lclTreeView;
		private ListView lclListView;
	}
}

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