ftp/Class1.cs

// Technical support: support@smartftp.com

using System;
using sfFTPLib;
using System.Threading;

namespace DotNetInteropDemo
{
    class Demo
    {
		// fields
		private FTPConnectionMTA _ftp;
		private UPnPNATManager _upnp;

		// methods
		public void Start()
		{
			sfFTPLib.Global global = new sfFTPLib.Global();

			// TODO: Uncomment and insert the serial to load license
			//global.LoadLicense("XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX");

			// Init UPnP
			//_upnp = new UPnPNATManager();
			//_upnp.Initialize();

			// Create our COM object through the Interop (Early Binding)
			_ftp = new FTPConnectionMTA();

			// Setup delegates  
			_ftp.OnSocketConnect += new sfFTPLib._IFTPConnectionEvents_OnSocketConnectEventHandler(OnSocketConnect);
			_ftp.OnSocketDisconnect += new sfFTPLib._IFTPConnectionEvents_OnSocketDisconnectEventHandler(OnSocketDisconnect);
			_ftp.OnTransferProgress += new sfFTPLib._IFTPConnectionEvents_OnTransferProgressEventHandler(OnTransferProgress);

			// Late Binding
			//Type t = Type.GetTypeFromProgID("sfFTPLib.FTPConnectionMTA");
			//System.Object obj = Activator.CreateInstance(t);
			//_ftp = obj as IFTPConnection;

			// host settings
			_ftp.Host = "ftp.smartftp.com";
			_ftp.Port = 21;
			_ftp.Username = "anonymous";
			_ftp.Password = "test@test.com";
			_ftp.Passive = true;
            //_ftp.Protocol = Protocol.ftpProtocolSSLExplicit;

            // client certificate
            //System.Array certificateThumbprint = Array.CreateInstance(typeof (byte), 20);
            // certificateThumbprint[0] = 0x12; 
            // certificateThumbprint[1] = 0x34; 
            // etc
            //_ftp.SSLSocketLayer.ClientCertThumbprint = certificateThumbprint;

			// proxy settings
			//_ftp.Proxy.Type = sfFTPLib.enumProxyType.ftpProxyTypeNone;
			//_ftp.FTPProxy.Type = sfFTPLib.enumFTPProxyType.ftpFTPProxyTypeNone;

			// set CLNT name
			//FTPClientId clientid = new FTPClientId();
			//clientid.CLNT = "SmartFTP Script";
			//_ftp.ClientId = clientid;
	
			// log everything 
			_ftp.LogFile.File = "DotNetInteropDemo.log";

			// connect to host
			_ftp.Connect();
			_ftp.ChangeDirectory("/SmartFTP");
			
			{
				sfFTPLib.FTPItems items = _ftp.ReadDirectory();
				// Use the foreach statement to iterate through 
				// elements in the collection 
				foreach (FTPItem objItem in items)
				{
					System.Console.WriteLine("Type={0}; Name={1}; Size={2}", objItem.Type, objItem.Name, objItem.Size);
				}
			}

			// download file
			// Note: No resume in this sample
			int nStartPosLo = 0;
			int nStartPosHi = 0;
			_ftp.DownloadFile("License.txt", "License.txt", nStartPosLo, nStartPosHi);

			System.Console.WriteLine("DownloadFile() successful.");
			System.Console.WriteLine("LastTransferBytes = {0} B", _ftp.LastTransferBytes);
			System.Console.WriteLine("LastTransferTime = {0} s", _ftp.LastTransferTime);
			System.Console.WriteLine("LastTransferSpeed = {0} B/s", _ftp.LastTransferSpeed);

			_ftp.Disconnect();

			// Uninit UPnP
			//_upnp.Uninitialize();
		}

        // Events
		public void OnSocketConnect()
		{
			System.Console.WriteLine("Event: OnConnect()");
		}

		public void OnSocketDisconnect()
		{
			System.Console.WriteLine("Event: OnDisconnect()");
		}

		public void OnTransferProgress(System.Int32 nTransferBytesLo, System.Int32 nTransferBytesHi)
		{
// 		Int64 nTransferBytes = nTransferBytesHi;
// 		nTransferBytes <<= 32;
// 		nTransferBytes += nTransferBytesLo;
// 
// 		System.Console.WriteLine("Event: OnTransferProgress(\"{0}\")", nLastTransferBytes);
      System.Console.WriteLine("Event: OnTransferProgress(\"{0}\")", _ftp.LastTransferBytes);
		}
	}

	/// <summary>
	/// Demo class for sfFTPLib
	/// </summary>
	class Class1
	{
		/// <summary>
		/// The main entry point for the application.
		/// </summary>
		[MTAThreadAttribute]
		static void Main(string[] args)
		{
			// Use GetApartmentState on .NET 2.x or higher
			System.Console.WriteLine("The apartment state is: {0}", Thread.CurrentThread.ApartmentState.ToString());

			Demo demo = new Demo();
			demo.Start();
		}
	}
}