Click here to Skip to main content
15,895,746 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.6K   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;
using System.Net.Sockets;
using System.Runtime.InteropServices;

namespace KCommon.Net.FTP
{
	#region COM interface IFtpFile
	public interface IFtpFile : IFtpItem
	{
		long	Size		{get;}
	}
	#endregion

	[ClassInterface(ClassInterfaceType.None)]
	public class FtpFile : IFtpFile
	{
		FtpDirectory		m_parent;
		string				m_name;
		string				m_permission;
		long				m_size;
		
		internal FtpFile(FtpDirectory parent, ref ItemInfo info)
		{
			m_parent	= parent;
			m_name		= info.name;
			m_size		= info.size;
			m_permission= info.permission;
		}
		internal FtpFile(FtpDirectory parent, string name)
		{
			m_parent	= parent;
			m_name		= name;
		}
		public bool IsFile
		{
			get {return true;}
		}
		public bool IsDirectory
		{
			get {return false;}
		}
		public string Name
		{
			get {return m_name;}
			set 
			{
				// Assume Parent is Current Directory. 
				// Will cause SERIOUS PROBLEM if Parent is not current directory.
				m_parent.RenameSubitem(this, value);
				m_name = value;
			}
		}
		public string FullName
		{
			get {return m_parent.FullName + m_name;}
		}
		public long Size
		{
			get {return m_size;}
		}
		public FtpDirectory Parent
		{
			get
			{
				m_parent.CheckSessionCurrentDirectory();
				return m_parent;
			}
		}
		public string FullPath
		{
			get {return m_parent.FullName + m_name;}
		}
		public void Rename(string newName)
		{
		}
		public FtpInputDataStream GetInputStream()
		{
			return (FtpInputDataStream)GetStream(0, TransferDirection.Download);
		}
		public FtpOutputDataStream GetOutputStream()
		{
			return (FtpOutputDataStream)GetStream(0, TransferDirection.Upload);
		}
		public FtpInputDataStream GetInputStream(long offset)
		{
			return (FtpInputDataStream)GetStream(offset, TransferDirection.Download);
		}
		public FtpOutputDataStream GetOutputStream(long offset)
		{
			return (FtpOutputDataStream)GetStream(offset, TransferDirection.Upload);
		}
		private FtpDataStream GetStream(long offset, TransferDirection dir)
		{
			m_parent.CheckSessionCurrentDirectory();
			SessionConnected session = m_parent.FtpSession;
			if(offset != 0) 
				session.ControlChannel.REST(offset);
			FtpDataStream stream = session.ControlChannel.GetPassiveDataStream(dir);
			try {
				if(dir == TransferDirection.Download)
					session.ControlChannel.RETR(m_name);
				else
					session.ControlChannel.STOR(m_name);
			}catch(Exception) {
				stream.Dispose();
				throw;
			}
			return stream;
		}
	}
}

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