sftp/Test.cs

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

namespace CSharp
{
    class Test
    {
        // fields
        private SFTPConnection _ftp = null;

        // methods
        public void Start()
        {
            // Load License Key
            sfFTPLib.Global global = new sfFTPLib.Global();

            string strKey = "";

            // New license key format
            strKey += "<?xml version=\"1.0\" ?>\r\n";
            strKey += "<License>\r\n";
            strKey += "<Version>2.0</Version>\r\n";
            strKey += "<Id>400012345</Id>\r\n";
            strKey += "<Name>name</Name>\r\n";
            strKey += "<Email>user@host.com</Email>\r\n";
            strKey += "<Company>company</Company>\r\n";
            strKey += "<Product>SmartFTP FTP Library</Product>\r\n";
            strKey += "<Features>\r\n";
            strKey += "<Feature>SFTP</Feature>\r\n";
            strKey += "</Features>\r\n";
            strKey += "<Users>1</Users>\r\n";
            strKey += "<Maintenance>2008-12-27</Maintenance>\r\n";
            strKey += "<Issue>2007-12-27</Issue>\r\n";
            strKey += "<Signature>xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx</Signature>\r\n";
            strKey += "</License>\r\n";
           
            if (global.LoadLicenseKeyData(strKey))
                System.Console.WriteLine("License key verified.");
            else
                System.Console.WriteLine("Failed to verify license key.");

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

            // Setup delegates  
            _ftp.OnTransferProgress += new sfFTPLib._ISFTPConnectionEvents_OnTransferProgressEventHandler(OnTransferProgress);

            // Late Binding
            //Type t = Type.GetTypeFromProgID("sfFTPLib.SFTPConnection");
            //System.Object obj = Activator.CreateInstance(t);
            //_ftp = obj as ISFTPConnection;

            // host settings
            _ftp.Host = "localhost";
            _ftp.Port = 22;
            _ftp.Username = "username";
            _ftp.Password = "password";
            _ftp.LogFile = "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)_ftp.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}\".", _ftp.Host);
            sfFTPLib.enumError err = _ftp.Connect();
            if (err == sfFTPLib.enumError.ftpErrorSuccess)
            {
                if (_ftp.RealPath(".") == sfFTPLib.enumError.ftpErrorSuccess)
                {
                    string strCurrentDirectory = _ftp.LastPath;
                    // read listing
                    if (_ftp.ReadDirectory(strCurrentDirectory) == sfFTPLib.enumError.ftpErrorSuccess)
                    {
                        SFTPItems items = _ftp.Items;
                        // Use the foreach statement to iterate through
                        // elements in the collection
                        foreach (SFTPItem 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;
                    if (_ftp.DownloadFile("License.txt", "License.txt", nStartPosLo, nStartPosHi, nStartPosLo, nStartPosHi) == sfFTPLib.enumError.ftpErrorSuccess)
                    {
                        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);
                    }
                    else
                    {
                        System.Console.WriteLine("LastStatusCode = {0}", _ftp.LastStatusCode);
                        System.Console.WriteLine("LastStatusMessage = {0}", _ftp.LastStatusMessage);
                    }
                }
                else
                {
                    System.Console.WriteLine("LastStatusCode = {0}", _ftp.LastStatusCode);
                    System.Console.WriteLine("LastStatusMessage = {0}", _ftp.LastStatusMessage);
                }
                _ftp.Disconnect();
            }
            else
            {
                if (err == sfFTPLib.enumError.ftpErrorLicense)
                    System.Console.WriteLine("Please acquire a license from http://www.smartftp.com");
                else
                    System.Console.WriteLine("Failed to connect.");
            }
        }

        // 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}\")", _ftp.LastTransferBytes);
        }
    }
}
 
© SmartSoft Ltd.