Click here to Skip to main content
15,886,806 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.Text;
using System.Collections;
using System.IO;

namespace HB.Web.Ftp
{
	/// <summary>
	/// Represents a file located on a FTP.
	/// </summary>
	public abstract class FileNode : IComparable, ICloneable
	{
		public FileNode()
		{
			filename = "";
			fullPath = "";
			isDir = false;
			dt = DateTime.MinValue;
			size = 0;
			dirName = "";
			ext = "";
		}

		/// <summary>
		/// Creates a new FTP file.
		/// </summary>
		/// <param name="name">The name of the file.</param>
		/// <param name="fullname">The full path of the file.</param>
		/// <param name="isDirectory">Indicates if the file is a directory.</param>
		/// <param name="lastWriteTime">The last time the file was written to.</param>
		/// <param name="length">The filesize.</param>
		/// <param name="ext">The file extension of the file.</param>
		/// <param name="dirName">The full path of the file's directory.</param>
		/// <exception cref="ArgumentNullException">When parameter name, fullname
		/// or dirName is null.</exception>
		public FileNode(string name, string fullname, bool isDirectory, 
			DateTime lastWriteTime, long length, string dirName, string ext)
		{
			if(name == null)
				throw new ArgumentNullException("name");
			if(fullname == null)
				throw new ArgumentNullException("fullname");
			if(dirName == null)
				throw new ArgumentNullException("dirName");
			
			filename = name;
			fullPath = fullname;
			isDir = isDirectory;
			dt = lastWriteTime;
			size = length;
			this.dirName = dirName;
			this.ext = ext;
		}

		/// <summary>
		/// Copy constructor.
		/// </summary>
		/// <param name="fileNode">The file node to copy.</param>
		/// <remarks>Performs a deep copy.</remarks>
		/// <exception cref="ArgumentNullException">When the parameter fileNode
		/// is null.</exception>
		public FileNode(FileNode fileNode)
		{
			if(fileNode == null)
				throw new ArgumentNullException("fileNode");
			dirName = fileNode.dirName;
			dt = fileNode.dt;
			filename = fileNode.filename;
			fullPath = fileNode.fullPath;
			isDir = fileNode.isDir;
			size = fileNode.size;
			ext = fileNode.ext;
		}
		
		static FileNode()
		{
			numFormat = new System.Globalization.NumberFormatInfo();
			numFormat.NumberDecimalDigits = 0;
		}

		/// <summary>
		/// Deletes a <see cref="FileNode"/> from the FTP server.
		/// </summary>
		/// <param name="list"></param>
		/// <param name="clientOutput"></param>
		/// <param name="serverOutput"></param>
		public abstract void Delete(DirectoryList list, TextWriter clientOutput, TextWriter serverOutput);

		/// <summary>
		/// Returns the subdirectories of the current directory.
		/// </summary>
		/// <param name="list"></param>
		/// <param name="clientOutput"></param>
		/// <param name="serverOutput"></param>
		/// <returns></returns>
		public abstract FileNode[] GetDirectories(DirectoryList list, TextWriter clientOutput, TextWriter serverOutput);

		/// <summary>
		/// Returns a file list from the current directory.
		/// </summary>
		/// <param name="list"></param>
		/// <param name="clientOutput"></param>
		/// <param name="serverOutput"></param>
		/// <returns></returns>
		public abstract FileNode[] GetFiles(DirectoryList list, TextWriter clientOutput, TextWriter serverOutput);

		/// <summary>
		/// Creates an array of UNIX file nodes from a FTP directory LISTing.
		/// </summary>
		/// <param name="list">The FTP directory LISTing.</param>
		/// <param name="path">The path where the FTP directory LISTing was performed.</param>
		/// <returns>UNIX file nodes representing each listing in the FTP directory LISTing.</returns>
		/// <remarks>No exception handling is performed.</remarks>
		public abstract FileNode[] FromFtpList(string list, string path);

		/// <summary>
		/// Gets the string representing the extension part of the file.
		/// </summary>
		public string Extension
		{
			get
			{
				return ext;
			}
		}

		public abstract FileNode Parse(string text, string absolutePath);

		/// <summary>
		/// Gets a value indicating whether the file node is a directory.
		/// </summary>
		public bool IsDirectory
		{
			get
			{
				return isDir;
			}
		}

		/// <summary>
		/// Gets a string representing the directory's full path.
		/// </summary>
		public string DirectoryName
		{
			get
			{
				return dirName;
			}
		}

		/// <summary>
		/// Gets the name of the file.
		/// </summary>
		public string Name
		{
			get
			{
				return filename;
			}
		}

		/// <summary>
		/// Gets the full path of the file.
		/// </summary>
		public string FullName
		{
			get
			{
				return fullPath;
			}
		}

		/// <summary>
		/// Gets the size of the file or directory.
		/// </summary>
		public long Length
		{
			get
			{
				return size;
			}
		}

		/// <summary>
		/// Gets the time when the current file or directory was last written to.
		/// </summary>
		public DateTime LastWriteTime
		{
			get
			{
				return dt;
			}
		}

		/// <summary>
		/// Compares the filename of two file nodes.
		/// </summary>
		/// <param name="obj">An object of type FileNode.</param>
		/// <returns></returns>
		/// <exception cref="ArgumentException">When parameter obj is not of type FileNode.</exception>
		public int CompareTo(object obj)
		{
			FileNode fn = obj as FileNode;
			if(fn == null)
				throw new ArgumentException("obj is not of type FileNode.", "obj");
			return fn.Name.CompareTo(Name);
		}

		/// <summary>
		/// Compares this instance of a <see cref="FileNode"/> with another object.
		/// </summary>
		/// <param name="obj">The <see cref="Object"/> to compare.</param>
		/// <returns>true if parameter obj is of type <see cref="FileNode"/> and is equal to
		/// this instance.</returns>
		public override bool Equals(object obj)
		{
			return ((obj is FileNode) && (this == (FileNode)obj));
		}

		/// <summary>
		/// Determines if two <see cref="FileNode"/> objects are equal.
		/// </summary>
		/// <param name="lhs">A <see cref="FileNode"/> to compare.</param>
		/// <param name="rhs">A <see cref="FileNode"/> to compare.</param>
		/// <returns>True if both <see cref="FileNode"/> objects are equal.</returns>
		/// <remarks>Equality is determined by comparing the <see cref="FileNode.FullName"/>
		/// of both parameters</remarks>
		public static bool operator ==(FileNode lhs, FileNode rhs)
		{
			if(((lhs as object) == null) && ((rhs as object) == null))
				return true;
			else if(((lhs as object) == null) || ((rhs as object) == null))
				return false;
			else
				return (lhs.FullName == rhs.FullName);
		}

		/// <summary>
		/// Determines if two <see cref="FileNode"/> objects are inequal.
		/// </summary>
		/// <param name="lhs">A <see cref="FileNode"/> to compare.</param>
		/// <param name="rhs">A <see cref="FileNode"/> to compare.</param>
		/// <returns>True if both <see cref="FileNode"/> objects are inequal.</returns>
		/// <remarks>Equality is determined by comparing the <see cref="FileNode.FullName"/>
		/// of both parameters</remarks>
		public static bool operator !=(FileNode lhs, FileNode rhs)
		{
			return !FileNode.Equals(lhs, rhs);
		}

		/// <summary>
		/// Returns the a string representation of the <see cref="FileNode"/>.
		/// </summary>
		/// <returns>The a string representation of the <see cref="FileNode"/>.</returns>
		/// <remarks>The string return mimcs the NT 'DIR' listing format. E.X:
		/// //06/06/2003  08:15p      &gt;DIR;&lt;          WINNT
		/// //08/12/2002  10:05p           2,359,352 nightelfboxart-1024x.bmp</remarks>
		public override string ToString()
		{	//06/06/2003  08:15p      <DIR>          WINNT
			//08/12/2002  10:05p           2,359,352 nightelfboxart-1024x.bmp
			StringBuilder line = new StringBuilder(LastWriteTime.ToString("MM/dd/yyyy  hh:mmt      ").ToLower());
			if(IsDirectory)
				line = line.Append("<DIR>          ");
			else
				line = line.Append(LeftPad(Length.ToString("n", numFormat), 14) + " ");
			line = line.Append(Name);
			return line.ToString();
		}

		/// <summary>
		/// Prepends a <see cref="String"/> with whitespace.
		/// </summary>
		/// <param name="value">The <see cref="String"/> to prepend with whitespaces.</param>
		/// <param name="length">The number of whitespaces to prepend the <see cref="String"/> with.</param>
		/// <returns>Parameter value with prepended whitespaces.</returns>
		/// <remarks>No exception handling is performed.</remarks>
		protected string LeftPad(string value, int length)
		{
			if(value.Length > length)
				value = value.Substring(0, 8);
			while(value.Length < length)
				value = " " + value;
			return value;
		}

		/// <summary>
		/// Appends a <see cref="String"/> with whitespace.
		/// </summary>
		/// <param name="value">The <see cref="String"/> to append with whitespaces.</param>
		/// <param name="length">The number of whitespaces to append the <see cref="String"/> with.</param>
		/// <returns>Parameter value with appended whitespaces.</returns>
		/// <remarks>No exception handling is performed.</remarks>
		protected string RightPad(string value, int length)
		{
			if(value.Length > length)
				value = value.Substring(0, 8);
			while(value.Length < length)
				value+=" ";
			return value;
		}

		/// <summary>
		/// Creates a deep copy of this instance of <see cref="FileNode"/>.
		/// </summary>
		/// <returns>A deep copy of this instance of <see cref="FileNode"/>.</returns>
		public abstract object Clone();

		/// <summary>
		/// Stores the value for the IsDirectory property.
		/// </summary>
		private bool isDir;

		/// <summary>
		/// Stores the value for the length property.
		/// </summary>
		private long size;

		/// <summary>
		/// Stores the value for the Name property.
		/// </summary>
		private string filename;

		/// <summary>
		/// Stores the value for file extension property.
		/// </summary>
		private string ext;
		
		/// <summary>
		/// Stores the value for the FullName property.
		/// </summary>
		private string fullPath;
		
		/// <summary>
		/// Stores the value for the DirectoryName property.
		/// </summary>
		private string dirName;
		
		/// <summary>
		/// Stores the value for the LastWriteTime property.
		/// </summary>
		private DateTime dt;

		private static System.Globalization.NumberFormatInfo numFormat;
	}
}

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