sftp/Test.cs
using System;
using System.Collections.Generic;
using System.Text;
using sfFTPLib;
namespace CSharp
{
class Test
{
// fields
private sfFTPLib.SSHConnection _connection = null;
private sfFTPLib.SFTPConnection _sftp = null;
// methods
public void Start()
{
sfFTPLib.Global global = new sfFTPLib.Global();
// TODO: insert the serial here
//global.LoadLicense("XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX");
// Create our COM object through the Interop (Early Binding)
_connection = new sfFTPLib.SSHConnection();
// Late Binding
//Type t = Type.GetTypeFromProgID("sfFTPLib.SFTPConnection");
//System.Object obj = Activator.CreateInstance(t);
//_ftp = obj as ISFTPConnection;
// host settings
_connection.Host = "localhost";
_connection.Port = 22;
_connection.Username = "username";
_connection.Password = "password";
_connection.LogFile.File = "CSharp.log";
// Limit authentications
enumSSHAuthentication[] authentications = new enumSSHAuthentication[2];
authentications[0] = sfFTPLib.enumSSHAuthentication.ftpSSHAuthenticationPassword;
authentications[1] = sfFTPLib.enumSSHAuthentication.ftpSSHAuthenticationNone;
object objAuthentications = (System.Array)authentications;
//_ftp.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();
//_ftp.PrivateKey = keymanager.LoadFile("Identity", "password");
// connect to host
System.Console.WriteLine("Connecting to \"{0}\".", _connection.Host);
_connection.Connect();
SFTPTest();
_connection.Disconnect();
}
// Events
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}\")", _sftp.LastTransferBytes);
}
public void SFTPTest()
{
_sftp = _connection.CreateSFTPConnection();
_sftp.Connect();
// Setup delegates
_sftp.OnTransferProgress += new sfFTPLib._ISFTPConnectionEvents_OnTransferProgressEventHandler(OnTransferProgress);
string strCurrentDirectory = _sftp.RealPath(".");
FTPItems items = _sftp.ReadDirectory(strCurrentDirectory);
// 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
ulong startPosition = 0;
ulong endPosition = 0;
// Use the absolute path to the source file
_sftp.DownloadFileEx("/License.txt", "License.txt", startPosition, endPosition);
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);
}
}
}