ftp/Class1.cs
// Technical support: support@smartftp.com
using System;
using sfFTPLib;
using System.Threading;
namespace DotNetInteropDemo
{
class Demo
{
private FTPConnectionMTA _ftp;
// methods
public void Start()
{
var global = new sfFTPLib.Global();
// If LoadLicense is not called, the component reads the product key from the Windows registry.
// The product key can be set using the supplied SetProductKey.cmd script from the installation folder.
// If no product key is found in the registry, e.g. during the trial period, 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: For manual activation, use the provided product key after purchasing 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";
// 21 is the default value
//_ftp.Port = 21;
_ftp.Username = "anonymous";
_ftp.Password = "test@test.com";
// _ftp.Passive = true;
_ftp.Protocol = FTPProtocol.ftpProtocolPreferTLS;
// if the FTP server (e.g. Microsoft FTP Service for IIS with virtual hosts) requires the HOST command, uncomment this line:
//_ftp.FEAT = FEATCommand.ftpFEATCommandEnableBeforeAndAfterLogin;
// 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;
// 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 (sfFTPLib.FTPItem item in items)
{
if (item.Type == sfFTPLib.ItemType.ftpItemTypeRegularFile)
{
System.Console.WriteLine("Type={0}; Name={1}; Size={2}", item.Type, item.Name, item.Size);
}
else
{
System.Console.WriteLine("Type={0}; Name={1}", item.Type, item.Name);
}
}
}
// 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();
}
}
}