ftp/Class1.cs

// Technical support: support@smartftp.com

using System;
using sfFTPLib;
using System.Threading;

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

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

            // If LoadLicense is not called a trial license is automatically obtained from the activation server. The FTP Library uses WinHTTP to access
            // the activation server at www.smartftp.com (TLS, port 443). Ensure that your application is not blocked by any firewall.
            // TODO: insert the provided serial after the purchase of a license
			//global.LoadLicense("XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX");

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

			// Setup delegates
			_ftp.OnDisconnect += new sfFTPLib._IFTPConnectionEvents_OnDisconnectEventHandler(OnSocketDisconnect);

			// 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
			// 20 byte certificate thumbprint which identifies the certificate
			//var certificateThumbprint = new byte[] { 0x12, 0x13, 0x14, 0x15, 0x16, 0x12, 0x13, 0x14, 0x15, 0x16, 0x12, 0x13, 0x14, 0x15, 0x16, 0x12, 0x13, 0x14, 0x15, 0x16 };
			//_ftp.SSLSocketLayer.ClientCertThumbprint = certificateThumbprint;

			// proxy settings
			//_ftp.Proxy.Type = sfFTPLib.ProxyType.ftpProxyTypeNone;
			//_ftp.FTPProxy.Type = sfFTPLib.FTPProxyType.ftpFTPProxyTypeNone;

			// set CLNT name
			//var clientId = new FTPClientId();
			//clientId.Name = "SmartFTP Script";
			//_ftp.ClientId = clientId;

			// log everything
			var fileLogger = _ftp.SetFileLogger();
			fileLogger.file = "DotNetInteropDemo.log";
			
			// connect to host
			_ftp.Connect();
			_ftp.ChangeDirectory("/");

			{
				var items = _ftp.ReadDirectory();
				foreach (var item in items)
				{
					System.Console.WriteLine("Type={0}; Name={1}; Size={2}", item.Type, item.Name, item.Size);
				}
			}

			// download file
			// Note: No resume in this sample
			ulong startPosition = 0;
			ulong endPosition = ulong.MaxValue;
			_ftp.DownloadFileEx(@"This is a DEMO server.txt", @"This is a DEMO server.txt", startPosition, endPosition, null);

			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);

			// Upload file
			//_ftp.UploadFileEx(@"c:\test.dat", @"test.dat", startPosition, null);

			_ftp.Disconnect();
		}

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

	/// <summary>
	/// Demo class for sfFTPLib
	/// </summary>
	class Class1
	{
		/// <summary>
		/// The main entry point for the application.
		/// </summary>
		[MTAThreadAttribute]
		static void Main(string[] args)
		{
			var demo = new Demo();
			demo.Start();
		}
	}
}