Click here to Skip to main content
15,886,578 members
Articles / Programming Languages / C#

Application for uploading modified Files to a FTP Server

Rate me:
Please Sign up or sign in to vote.
4.73/5 (16 votes)
14 Oct 2002CPOL2 min read 200.2K   4.4K   80  
Simple C# Console Application that uses a local MS-Access Database to store modification Dates of Files and uploads modified Files to a FTP Server
using System;
using System.Runtime.InteropServices;

namespace KCommon.Net.FTP
{
	public delegate void FtpCommandEventHandler(object sender, FtpCommandEventArgs args);
	public delegate void FtpResponseEventHandler(object sender, FtpResponseEventArgs args);
	public delegate void FtpFileEventHandler(object sender, IFtpFileTransferArgs args);

	#region COM Interface IFtpSession
	public interface IFtpSession
	{
		string			Server				{get; set;}
		int				Port				{get; set;}
		FtpDirectory	CurrentDirectory	{get; set;}
		FtpDirectory	RootDirectory		{get;}
		bool			IsBusy				{get;}
		bool			IsConnected			{get;}
		void AbortTransfer();
		void Connect(string user, string pass);
		void Close();
	}
	#endregion
	#region COM Event Interface IFtpEvents
	[InterfaceType(ComInterfaceType.InterfaceIsIDispatch)]
	public interface IFtpEvents 
	{
		void BeginPutFile(object sender, IFtpFileTransferArgs args);
		void EndPutFile(object sender, IFtpFileTransferArgs args);
		void BeginGetFile(object sender, IFtpFileTransferArgs args);
		void EndGetFile(object sender, IFtpFileTransferArgs args);
		void FileTransferProgress(object sender, IFtpFileTransferArgs args);
		void ResponseReceived(object sender, FtpResponseEventArgs args);
		void CommandSent(object sender, FtpCommandEventArgs args);
	}
	#endregion

	[
		Guid("0ED287BC-9F27-4c7e-931B-01AE0FC0BB02"),	
		ComSourceInterfaces(typeof(IFtpEvents)),
		ClassInterface(ClassInterfaceType.None)
	]
	public class Session : IFtpSession
	{
		SessionState		m_state;

		public Session() 
		{
			m_state			= new SessionDisconnected(this);
		}
		
		public string Server
		{
			get {return m_state.Server;}
			set {m_state.Server = value;}
		}

		public int Port
		{
			set {m_state.Port = value;}
			get {return m_state.Port;}
		}
	
		public FtpDirectory CurrentDirectory
		{
			get {return m_state.CurrentDirectory;}
			set {m_state.CurrentDirectory = value;}
		}
		
		public FtpDirectory RootDirectory
		{
			get {return m_state.RootDirectory;}
		}
		
		public ControlChannel ControlChannel
		{
			get {return m_state.ControlChannel;}
		}
		
		public bool IsConnected
		{
			get {return m_state.GetType() == typeof(SessionConnected);}
		}
		public bool IsBusy
		{
			get {return m_state.IsBusy;}
		}
		
		public void AbortTransfer()
		{
			m_state.AbortTransfer();
		}
		
		public void Connect(string user, string pass) 
		{
			m_state.Connect(user, pass);
		}
		
		public void Close()
		{
			m_state.Close();
		}
		
		internal SessionState State
		{
			set {m_state = value;}
			get {return m_state;}
		}
		
		public event FtpFileEventHandler		BeginPutFile;
		public event FtpFileEventHandler		EndPutFile;
		public event FtpFileEventHandler		BeginGetFile;
		public event FtpFileEventHandler		EndGetFile;
		public event FtpFileEventHandler		FileTransferProgress;
		public event FtpResponseEventHandler	ResponseReceived;
		public event FtpCommandEventHandler		CommandSent;


		#region Itnernal raise event routines
		internal void RaiseResponseEvent(string response)
		{
			if(ResponseReceived != null)
				ResponseReceived(this, new FtpResponseEventArgs(response));
		}

		internal void RaiseCommandEvent(string command)
		{
			if(CommandSent != null)
				CommandSent(this, new FtpCommandEventArgs(command));
		}
		internal void RaiseBeginPutFileEvent(IFtpFileTransferArgs args)
		{
			if(BeginPutFile != null)
				BeginPutFile(this, args);
		}
		internal void RaiseEndPutFile(IFtpFileTransferArgs args)
		{
			if(EndPutFile != null)
				EndPutFile(this, args);
		}

		internal void RaiseBeginGetFileEvent(IFtpFileTransferArgs args)
		{
			if(BeginGetFile != null)
				BeginGetFile(this, args);
		}
		internal void RaiseEndGetFile(IFtpFileTransferArgs args)
		{
			if(EndGetFile != null)
				EndGetFile(this, args);
		}
		internal void RaiseFileTransferProgressEvent(IFtpFileTransferArgs args)
		{
			if(FileTransferProgress != null) 
				FileTransferProgress(this, args);
		}
		#endregion
	}

	#region Ftp State base class
	internal class SessionState
	{
		public virtual IntPtr FtpHandle
		{
			get {throw new InvalidOperationException();}
		}
		public virtual string Server 
		{
			set {throw new InvalidOperationException();}
			get {return "";}
		}
		public virtual int Port
		{
			get {throw new InvalidOperationException();}
			set {throw new InvalidOperationException();}
		}
		public virtual FtpDirectory	CurrentDirectory
		{
			set{throw new InvalidOperationException();}
			get{throw new InvalidOperationException();}
		}
		public virtual FtpDirectory RootDirectory
		{
			get{throw new InvalidOperationException();}
		}
		public virtual ControlChannel ControlChannel
		{
			get{throw new InvalidOperationException();}
		}
		public virtual bool IsBusy
		{
			get {throw new InvalidOperationException();}
		}
		public virtual void AbortTransfer()
		{
			throw new InvalidOperationException();
		}
		public virtual void Connect(string user, string pass)
		{
			throw new InvalidOperationException();
		}
		public virtual void Close()
		{
			//throw new InvalidOperationException();
		}
	}
	#endregion
}

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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Chief Technology Officer Zeta Software GmbH
Germany Germany
Uwe does programming since 1989 with experiences in Assembler, C++, MFC and lots of web- and database stuff and now uses ASP.NET and C# extensively, too. He has also teached programming to students at the local university.

➡️ Give me a tip 🙂

In his free time, he does climbing, running and mountain biking. In 2012 he became a father of a cute boy and in 2014 of an awesome girl.

Some cool, free software from us:

Windows 10 Ereignisanzeige  
German Developer Community  
Free Test Management Software - Intuitive, competitive, Test Plans.  
Homepage erstellen - Intuitive, very easy to use.  
Offline-Homepage-Baukasten

Comments and Discussions