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()
{
sfFTPLib.Global 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
//FTPClientId 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("/");
{
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();
}
// 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)
{
Demo demo = new Demo();
demo.Start();
}
}
}