sftp/Test.cs

using sfFTPLib;
using System;
using System.Collections.Generic;
using System.Text;

namespace CSharp
{
    internal class Test
	{
		// fields
		private sfFTPLib.SSHConnection _connection = null;

		private sfFTPLib.SFTPConnection _sftp = null;

		// 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)
			_connection = new sfFTPLib.SSHConnection();

			// host settings
			_connection.Host = "localhost";
			_connection.Port = 22;
			_connection.Username = "username";
			_connection.Password = "password";

			var fileLogger = _connection.SetFileLogger();
			fileLogger.file = "ssh.log";

			// Limit authentications
			var authentications = new SSHAuthentication[2];
			authentications[0] = sfFTPLib.SSHAuthentication.ftpSSHAuthenticationPassword;
			authentications[1] = sfFTPLib.SSHAuthentication.ftpSSHAuthenticationNone;
			object objAuthentications = (System.Array)authentications;
			//_connection.set_Authentications(ref objAuthentications);

			// This snippet shows how to read the array
			System.Array arrayAuthentications = (System.Array)_connection.get_Authentications();
			for (int i = 0; i < arrayAuthentications.Length; i++)
			{
				arrayAuthentications.GetValue(i);
			}

			// load private key
			//sfFTPLib.KeyManager keymanager = new sfFTPLib.KeyManager();
			// From file
			//_connection.PrivateKey = keymanager.LoadFile("Identity", "password");
			//
			// From current users's certificate store
			// The thumbprint can be obtained from the certificate properties:
			// 1. Start certmgr.msc
			// 2. Go to the Personal\Certificates store
			// 3. Double click the certificate
			// 4. Go to the Details tab
			// 5. Select the Thumbprint field
			//_connection.PrivateKey = keymanager.LoadFromCertificateStore("dc3c1e61ac08b37e14a1025145e5c63bac2b7d05");

			// connect to host
			System.Console.WriteLine("Connecting to \"{0}\".", _connection.Host);
			_connection.Connect();
			SFTPTest();
			_connection.Disconnect();
		}

		public void SFTPTest()
		{
			_sftp = _connection.CreateSFTPConnection();
			var fileLogger = _sftp.SetFileLogger();
			fileLogger.file = "sftp.log";

			_sftp.Connect();

			var homeDirectory = _sftp.RealPath(".");
			var items = _sftp.ReadDirectory(homeDirectory);
			foreach (var item in items)
			{
				var line = string.Format("Type={0}; Name={1};", item.Type, item.Name);
				if (item.Type == sfFTPLib.ItemType.ftpItemTypeRegularFile)
				{
					line += string.Format(" Size={0}", item.Size);
				}
				System.Console.WriteLine(line);
			}

			// download file
			// Note: No resume in this sample
			ulong startPosition = 0;
			ulong endPosition = 0;
			// Use the absolute path to the source file
			_sftp.DownloadFileEx("/License.txt", "License.txt", sfFTPLib.DataTransferType.ftpDataTransferTypeImage, startPosition, endPosition, (int)sfFTPLib.DownloadFlags.ftpDownloadFlagReadBeyondEnd, null);
			System.Console.WriteLine("DownloadFile() successful.");
			System.Console.WriteLine("LastTransferBytes = {0} B", _sftp.LastTransferBytes);
			System.Console.WriteLine("LastTransferTime = {0} s", _sftp.LastTransferTime);
			System.Console.WriteLine("LastTransferSpeed = {0} B/s", _sftp.LastTransferSpeed);

			// upload file
			//_sftp.UploadFileEx(@"c:\test.dat", @"/remotefolder/test.dat", sfFTPLib.DataTransferType.ftpDataTransferTypeImage, startPosition, null)
		}
	}
}