Click here to Skip to main content
15,881,092 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 200K   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.IO;

namespace KCommon.Net.FTP
{
	internal class SessionConnected : SessionState
	{
		private Session			m_host;
		private ControlChannel	m_ctrlChannel;
		private FtpDirectory	m_root;
		private FtpDirectory	m_current;
		private FtpDataStream	m_dataStream;

		internal SessionConnected(Session h, ControlChannel ctrl)
		{
			m_host			= h;
			m_ctrlChannel	= ctrl;
            m_ctrlChannel.Session = this;			
		}
		internal void InitRootDirectory()
		{
			m_root		= new FtpDirectory(this);
			m_current	= m_root;
		}
		override public int Port
		{
			get {return m_ctrlChannel.Port;}
		}
		override public FtpDirectory RootDirectory
		{
			get	{return m_root;}
		}
		override public FtpDirectory CurrentDirectory
		{
			get	{return m_current;}
			set
			{
				m_ctrlChannel.CWD(value.FullName);
				m_current = value;
				m_current.ClearItems();
			}
		}
		override public bool IsBusy
		{
			get {return m_dataStream != null;}
		}
		
		internal Session Host
		{
			get {return m_host;}
		}

		public override ControlChannel ControlChannel
		{
			get {return m_ctrlChannel;}
		}

		internal void BeginDataTransfer(FtpDataStream stream)
		{
			lock(this) {
				if(m_dataStream != null)
					throw new FtpDataTransferException();
				m_dataStream = stream;
			}
		}
		internal void EndDataTransfer()
		{
			lock(this) {
				if(m_dataStream == null)
					throw new InvalidOperationException();
				m_dataStream = null;
			}
		}
		// You can only aborting file transfer started by
		// BeginPutFile and BeginGetFile
		override public void AbortTransfer()
		{
			// Save a copy of m_dataStream since it will be set 
			// to null when FtpDataStream call EndDataTransfer
            FtpDataStream tempDataStream = m_dataStream;
			if(tempDataStream != null) {
				tempDataStream.Abort();
                while(!tempDataStream.IsClosed)
					System.Threading.Thread.Sleep(0);
			}
		}

		override public void Close()
		{
			m_host.State	= new SessionDisconnected(m_host);
			m_host.Server	= m_ctrlChannel.Server;
			m_host.Port		= m_ctrlChannel.Port;
			try {
				m_ctrlChannel.Quit();
			}catch(IOException){
				return;
			}catch(FtpException){
				return;
			}finally {
				m_ctrlChannel.Close();
			}
		}
	}
}

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