Click here to Skip to main content
15,895,084 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.5K   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.Collections;
using System.Runtime.InteropServices;

namespace KCommon.Net.FTP
{
	#region COM interface IFtpResponse
	public interface IFtpResponse
	{
		string	Message {get;}
		int		Code	{get;}
	}
	#endregion

	[ClassInterface(ClassInterfaceType.None)]
	public class FtpResponse : IFtpResponse
	{
		private readonly Queue		m_responses;
		private readonly int		m_code;

		public const int InvalidCode						=  -1;
		public const int DataChannelOpenedTransferStart		= 125;
		public const int FileOkBeginOpenDataChannel			= 150;
		public const int ServiceReady						= 220;
		public const int ClosingDataChannel					= 226;
		public const int EnterPassiveMode					= 227;
		public const int RequestFileActionComplete			= 250;
		public const int UserLoggedIn						= 230;
		public const int UserAcceptedWaitingPass			= 331;
		public const int RequestFileActionPending			= 350;
		public const int ServiceUnavailable					= 421;
		public const int TransferAborted					= 426;
		

		internal FtpResponse(Stream m)
		{
			TextReader reader	= new StreamReader(m);
			m_responses			= new Queue();
			while(true) {
				string response = GetLine(reader);
				try {
					m_code = InvalidCode;
					m_code = int.Parse(response.Substring(0, 3));
				}catch {
					throw new FtpException("Invalid response", this);
				}
				m_responses.Enqueue(response);
				if(response.Length >= 4 && response[3] == '-')
					continue;
				break;
			}
			if(m_code == ServiceUnavailable)
				throw new FtpServerDownException(this);
		}

		public string Message
		{
			get {return (string)m_responses.Peek();}
		}

		public Queue Respones
		{
			get {return m_responses;}
		}

		public int Code
		{
			get {return m_code;}
		}

		private char ReadAppendChar(TextReader r, System.Text.StringBuilder toAppend)
		{
			char c = (char)r.Read();
			toAppend.Append(c);
			return c;
		}

		private string GetLine(TextReader reader)
		{
			System.Text.StringBuilder buff = new System.Text.StringBuilder();

			while(true) {
				while(ReadAppendChar(reader, buff) != '\r');
				while(ReadAppendChar(reader, buff) == '\r');
				if(buff[buff.Length-1] == '\n')
					break;
			}
			return buff.ToString();
		}
	}
}

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