edtFTPnet/PRO



public void Run(string serverAddress, int serverPort, string userName, 
                string password, string fileName)
{
    // Create SecureFTPConnection object
    SecureFTPConnection ftpConnection = new SecureFTPConnection();
    
    // setting server address and credentials
    ftpConnection.ServerAddress = serverAddress;
    ftpConnection.ServerPort = serverPort; 
    ftpConnection.UserName = userName;
    ftpConnection.Password = password;
    
    // connect to the server
    PrintLine("Queuing connection to " + serverAddress);
    ftpConnection.BeginConnect(new AsyncCallback(OnConnect), ftpConnection);

    // upload it to the server
    PrintLine("Queuing UploadFile");
    ftpConnection.BeginUploadFile(fileName, fileName, new AsyncCallback(OnUpload), ftpConnection);

    // get the size of the file we've just uploaded
    PrintLine("Queuing GetSize");
    ftpConnection.BeginGetSize(fileName, new AsyncCallback(OnGetSize), ftpConnection);

    // download it with a new name
    string newFile = fileName + ".copy";
    PrintLine("Queuing DownloadFile");
    ftpConnection.BeginDownloadFile(newFile, fileName, new AsyncCallback(OnDownload), ftpConnection);

    // delete the file on the server
    PrintLine("Queuing DeleteFile");
    ftpConnection.BeginDeleteFile(fileName, new AsyncCallback(OnDelete), ftpConnection);

    // Shut down client
    PrintLine("Queuing Close");
    ftpConnection.BeginClose(new AsyncCallback(OnClose), ftpConnection);

    // Wait for completion of Close operation
    PrintLine("All operations queued.  Waiting for signal");
    Print();
    signaller.WaitOne();

    PrintLine("Example complete");
}

private void OnConnect(IAsyncResult ar)
{
    SecureFTPConnection c = (SecureFTPConnection)ar.AsyncState;
    c.EndConnect(ar);
    PrintLine("Connect completed");
}

private void OnUpload(IAsyncResult ar)
{
    SecureFTPConnection c = (SecureFTPConnection)ar.AsyncState;
    c.EndUploadFile(ar);
    PrintLine("Upload completed");
}

private void OnGetSize(IAsyncResult ar)
{
    SecureFTPConnection c = (SecureFTPConnection)ar.AsyncState;
    long size = c.EndGetSize(ar);
    PrintLine("GetSize completed: size = " + size + " bytes");
}

private void OnDownload(IAsyncResult ar)
{
    SecureFTPConnection c = (SecureFTPConnection)ar.AsyncState;
    c.EndDownloadFile(ar);
    PrintLine("Download completed");
}

private void OnDelete(IAsyncResult ar)
{
    SecureFTPConnection c = (SecureFTPConnection)ar.AsyncState;
    c.EndDeleteFile(ar);
    PrintLine("Delete completed");
}

private void OnClose(IAsyncResult ar)
{
    SecureFTPConnection c = (SecureFTPConnection)ar.AsyncState;
    c.EndClose(ar);
    PrintLine("Close completed.  Signalling main thread");
    Print();
    signaller.Set();
}

private ManualResetEvent signaller = new ManualResetEvent(false);
public void Run(string serverAddress, int serverPort, string userName, string password)
{
    // Create SecureFTPConnection object
    SecureFTPConnection ftpConnection = new SecureFTPConnection();
    
    // setting server address and credentials
    ftpConnection.ServerAddress = serverAddress;
    ftpConnection.ServerPort = serverPort; 
    ftpConnection.UserName = userName;
    ftpConnection.Password = password;

    // connect to the server
    // By default, auto-login is on so Login doesn't need to be called
    PrintLine("Connecting to server " + serverAddress);
    ftpConnection.Connect();

    // get the current working directory and files
    PrintLine("Working directory is " + ftpConnection.ServerDirectory);

    // Shut down client
    PrintLine("Closing client");
    ftpConnection.Close();

    PrintLine("Example complete");
}
public void Run(string serverAddress, int serverPort, string userName, string password,
                string subdirectory)
{
    // Create SecureFTPConnection object
    SecureFTPConnection ftpConnection = new SecureFTPConnection();
    
    // setting server address and credentials
    ftpConnection.ServerAddress = serverAddress;
    ftpConnection.ServerPort = serverPort; 
    ftpConnection.UserName = userName;
    ftpConnection.Password = password;
    
    // connect to server
    PrintLine("Connecting to server " + serverAddress);
    ftpConnection.Connect();

    // get the current working directory
    PrintLine("Initial directory is " + ftpConnection.ServerDirectory);

    // change directory
    ftpConnection.ChangeWorkingDirectory(subdirectory);
    PrintLine("Changed to subdirectory " + ftpConnection.ServerDirectory);

    // go back to the parent directory
    ftpConnection.ChangeWorkingDirectoryUp();
    PrintLine("Changed back to original directory " + ftpConnection.ServerDirectory);

    // Shut down client
    PrintLine("Closing client");
    ftpConnection.Close();

    PrintLine("Example complete");
}
public void Run(string serverAddress, int serverPort, string userName, string password)
{
    // Create SecureFTPConnection object
    SecureFTPConnection ftpConnection = new SecureFTPConnection();
            
    // setting server address and credentials
    ftpConnection.ServerAddress = serverAddress;
    ftpConnection.ServerPort = serverPort; 
    ftpConnection.UserName = userName;
    ftpConnection.Password = password;
    
    // connect to server
    PrintLine("Connecting to server " + serverAddress);
    ftpConnection.Connect();

    // get the current working directory and files
    PrintLine("Working directory is " + ftpConnection.ServerDirectory);

    // get a simple string listing
    PrintLine("Simple listing");
    string[] files = ftpConnection.GetFiles();
    foreach (string file in files)
    {
            PrintLine("  " + file);
    }

    // get file details
    PrintLine("Detailed listing");
    FTPFile[] fileDetails = ftpConnection.GetFileInfos();
    foreach (FTPFile file in fileDetails)
    {
        if (file.Dir)
            PrintLine("  " + file.Name + " (directory)");
        else
            PrintLine("  " + file.Name + " (" + file.Size + " bytes)");
    }

    // Shut down client
    PrintLine("Closing client");
    ftpConnection.Close();

    PrintLine("Example complete");
}
public void Run(string serverAddress, int serverPort, string userName, string password)
{
    // Create SecureFTPConnection object
    SecureFTPConnection ftpConnection = new SecureFTPConnection();
    
    // setting server address and credentials
    ftpConnection.ServerAddress = serverAddress;
    ftpConnection.ServerPort = serverPort; 
    ftpConnection.UserName = userName;
    ftpConnection.Password = password;

    // hook up event-handlers
    ftpConnection.Connected += new EnterpriseDT.Net.Ftp.FTPConnectionEventHandler(ftpConnection_Connected);
    ftpConnection.Closed += new EnterpriseDT.Net.Ftp.FTPConnectionEventHandler(ftpConnection_Closed);
    
    // connect to server
    PrintLine("Connecting to server " + serverAddress);
    ftpConnection.Connect();

    // get the current working directory and files
    PrintLine("Working directory is " + ftpConnection.ServerDirectory);
    
    // Shut down client
    PrintLine("Closing client");
    ftpConnection.Close();

    PrintLine("Example complete");
}

private void ftpConnection_Connected(object sender, EnterpriseDT.Net.Ftp.FTPConnectionEventArgs e)
{
    PrintLine("Connected to " + e.ServerAddress + ":" + e.ServerPort);
}

private void ftpConnection_Closed(object sender, EnterpriseDT.Net.Ftp.FTPConnectionEventArgs e)
{
    PrintLine("Disconnected from " + e.ServerAddress + ":" + e.ServerPort);
}
public void Run(string serverAddress, int serverPort, string userName, 
                string password, string fileName)
{
    // Create SecureFTPConnection object
    SecureFTPConnection ftpConnection = new SecureFTPConnection();
    
    // setting server address and credentials
    ftpConnection.ServerAddress = serverAddress;
    ftpConnection.ServerPort = serverPort; 
    ftpConnection.UserName = userName;
    ftpConnection.Password = password;
    
    // connect to server
    PrintLine("Connecting to server " + serverAddress);
    ftpConnection.Connect();

    // get the current working directory and files
    PrintLine("Working directory is " + ftpConnection.ServerDirectory);

    // upload it to the server
    ftpConnection.UploadFile(fileName, fileName);
    PrintLine("Uploaded file '" + fileName + "'");

    // download it with a new name
    string newFile = fileName + ".copy";
    ftpConnection.DownloadFile(newFile, fileName);
    PrintLine("Downloaded file '" + newFile + "'");

    // delete the file on the server
    ftpConnection.DeleteFile(fileName);
    PrintLine("Deleted file from server '" + fileName + "'");

    // Shut down client
    PrintLine("Closing client");
    ftpConnection.Close();

    PrintLine("Example complete");
}
public void Run(string serverAddress, int serverPort, string userName, string password, 
                string proxyAddress, int proxyPort, string proxyUserName, string proxyPassword)
{
    // Create SecureFTPConnection object
    SecureFTPConnection ftpConnection = new SecureFTPConnection();
            
    // setting server address and credentials
    ftpConnection.ServerAddress = serverAddress;
    ftpConnection.ServerPort = serverPort; 
    ftpConnection.UserName = userName;
    ftpConnection.Password = password;

    // set up SOCKS server
    Socks5Context socks = new Socks5Context(proxyAddress, proxyPort);
    socks.AuthMethods.Add(new Socks5UserNamePasswordAuthMethod(proxyUserName, proxyPassword));
    ftpConnection.SocksProxySettings = socks;
    
    // connect to server
    PrintLine("Connecting to server " + serverAddress);
    ftpConnection.Connect();

    // get the current working directory and files
    PrintLine("Files in directory " + ftpConnection.ServerDirectory + ":");
    PrintLines(ftpConnection.GetFiles());
    
    // Shut down client
    PrintLine("Closing client");
    ftpConnection.Close();

    PrintLine("Example complete");
}
public void Run(string serverAddress, int serverPort, string userName, string password, string fileName)
{
    // Create SecureFTPConnection object
    SecureFTPConnection ftpConnection = new SecureFTPConnection();

    // setting server address and credentials
    ftpConnection.ServerAddress = serverAddress; 
    ftpConnection.ServerPort = serverPort; 
    ftpConnection.UserName = userName;
    ftpConnection.Password = password;
    
    // connect to server
    PrintLine("Connecting to server " + serverAddress);
    ftpConnection.Connect();

    // get data to be transferred
    string s = "Hello world";
    byte[] bytes = Encoding.ASCII.GetBytes(s);

    // upload the byte-array to a file on the server
    PrintLine("Uploading byte-array to the file '" + fileName + "'");
    ftpConnection.UploadByteArray(bytes, fileName);

    // download byte-array and display it
    PrintLine("Downloading the file '" + fileName + "' into a byte-array");
    bytes = ftpConnection.DownloadByteArray(fileName);
    PrintLine("Downloaded string = " + Encoding.ASCII.GetString(bytes));

    // build StringStream (defined below) for "Hello world"
    StringStream inStr = new StringStream(s);

    // upload the stream to a file on the server
    PrintLine("Uploading stream to the file '" + fileName + "'");
    ftpConnection.UploadStream(inStr, fileName);
    inStr.Close();

    // create a StringStream to download into
    StringStream outStr = new StringStream();
    PrintLine("Downloading the file '" + fileName + "' into a stream");
    ftpConnection.DownloadStream(outStr, fileName);
    PrintLine("Downloaded string = " + outStr.ToString());
    outStr.Close();
    
    // Shut down client
    PrintLine("Closing client");
    ftpConnection.Close();

    PrintLine("Example complete");
}

class StringStream : Stream
{
    private MemoryStream buffer;
    
    public StringStream()
    {
        buffer = new MemoryStream();
    }

    public StringStream(string str)
    {
        buffer = new MemoryStream(Encoding.ASCII.GetBytes(str));
    }

    public override int Read(byte[] bytes, int offset, int count)
    {
        return buffer.Read(bytes, offset, count);
    }

    public override void Write(byte[] bytes, int offset, int count)
    {
        buffer.Write(bytes, offset, count);
    }

    public override long Seek(long offset, SeekOrigin origin)
    {
        return buffer.Seek(offset, origin);
    }

    public override string ToString()
    {
        return Encoding.ASCII.GetString(buffer.GetBuffer());
    }

    public override bool CanRead
    {
        get
        {
            return true;
        }
    }

    public override bool CanWrite
    {
        get
        {
            return true;
        }
    }

    public override bool CanSeek
    {
        get
        {
            return true;
        }
    }

    public override void Flush()
    {
        buffer.Flush();
    }

    public override long Length
    {
        get
        {
            return buffer.Length;
        }
    }

    public override void SetLength(long value)
    {
        buffer.SetLength(value);
    }

    public override long Position
    {
        get
        {
            return buffer.Position;
        }
        set
        {
            buffer.Position = value;
        }
    }
}
public void Run(string serverAddress, int serverPort, string userName, string password, 
                string directory, string wildcard)
{
    // Create SecureFTPConnection object
    SecureFTPConnection ftpConnection = new SecureFTPConnection();

    // setting server address and credentials
    ftpConnection.ServerAddress = serverAddress;
    ftpConnection.ServerPort = serverPort; 
    ftpConnection.UserName = userName;
    ftpConnection.Password = password;

    // connect to the server
    // By default, auto-login is on so Login doesn't need to be called
    PrintLine("Connecting to server " + serverAddress);
    ftpConnection.Connect();

    // add an event-handler so we can see what files were downloaded
    ftpConnection.Downloaded += new FTPFileTransferEventHandler(ftpConnection_Downloaded);

    // download the files using the wildcard
    ftpConnection.DownloadMultiple(directory, wildcard);

    // Shut down client
    PrintLine("Closing client");
    ftpConnection.Close();

    PrintLine("Example complete");
}

// event-handler printing the name and size of a downloaded file
private void ftpConnection_Downloaded(object sender, FTPFileTransferEventArgs e)
{
    PrintLine("Downloaded file " + e.RemoteFile + " of size " + e.FileSize);
}
public void Run(string serverAddress, int serverPort, string userName, string password, 
                string directory, int maxSize)
{
    // Create SecureFTPConnection object
    SecureFTPConnection ftpConnection = new SecureFTPConnection();

    // setting server address and credentials
    ftpConnection.ServerAddress = serverAddress;
    ftpConnection.ServerPort = serverPort; 
    ftpConnection.UserName = userName;
    ftpConnection.Password = password;

    // remember the maximum size we're allowing
    this.maxSize = maxSize;

    // connect to the server
    // By default, auto-login is on so Login doesn't need to be called
    PrintLine("Connecting to server " + serverAddress);
    ftpConnection.Connect();

    // add an event-handler so we can see what files were downloaded
    ftpConnection.Downloaded += new FTPFileTransferEventHandler(ftpConnection_Downloaded);

    // download the files using the filter
    ftpConnection.DownloadMultiple(directory, new FileFilter(IsFileSmall));

    // Shut down client
    PrintLine("Closing client");
    ftpConnection.Close();

    PrintLine("Example complete");
}

// stores the maximum size as used in the IsFileSmall method
private long maxSize;

// filtering method which returns true if the file is less than maxSize
public bool IsFileSmall(FTPFile file)
{
    return file.Size<=maxSize;
}

// event-handler printing the name and size of a downloaded file
private void ftpConnection_Downloaded(object sender, FTPFileTransferEventArgs e)
{
    PrintLine("Downloaded file " + e.RemoteFile + " (" + e.FileSize + " bytes)");
}
public void Run(string serverAddress, int serverPort, string userName, string password)
{
    // Create SecureFTPConnection object
    SecureFTPConnection ftpConnection = new SecureFTPConnection();
    
    // setting server address and credentials
    ftpConnection.ServerAddress = serverAddress;
    ftpConnection.ServerPort = serverPort; 
    ftpConnection.UserName = userName;
    ftpConnection.Password = password;
    
    // select explicit FTPS
    ftpConnection.Protocol = FileTransferProtocol.FTPSExplicit;
    
    // switch off server validation (unsafe - only do this when testing)
    ftpConnection.ServerValidation = SecureFTPServerValidationType.None;
    
    // connect to server
    PrintLine("Connecting to server " + serverAddress);
    ftpConnection.Connect();
    
    // get the current working directory and files
    PrintLine("Files in directory " + ftpConnection.ServerDirectory + ":");
    PrintLines(ftpConnection.GetFiles());
    
    // Shut down client
    PrintLine("Closing client");
    ftpConnection.Close();
    
    PrintLine("Example complete");
}
public void Run(string serverAddress, int serverPort, string userName, string password)
{
    // Create SecureFTPConnection object
    SecureFTPConnection ftpConnection = new SecureFTPConnection();
    
    // setting server address and credentials
    ftpConnection.ServerAddress = serverAddress;
    ftpConnection.ServerPort = serverPort; 
    ftpConnection.UserName = userName;
    ftpConnection.Password = password;
    
    // select explicit FTPS
    ftpConnection.Protocol = FileTransferProtocol.FTPSExplicit;
    
    // show certificate manager to allow user to install the server's
    // certificate or the certificate of the CA that issued it.
    PrintLine("Displaying certificate manager");
    SSLFTPCertificate.ShowCertificateManager("Please ensure that the server's certificate is installed", null);
    
    // select automatic validation of the server's certificate.  The certificate, 
    // or that of the CA that issued it, must be installed into the operating 
    // system's certificate store before this example will work.
    ftpConnection.ServerValidation = SecureFTPServerValidationType.Automatic;
    
    // connect to server
    PrintLine("Connecting to server " + serverAddress);
    ftpConnection.Connect();
    
    // get the current working directory and files
    PrintLine("Files in directory " + ftpConnection.ServerDirectory + ":");
    PrintLines(ftpConnection.GetFiles());
    
    // Shut down client
    PrintLine("Closing client");
    ftpConnection.Close();
    
    PrintLine("Example complete");
}
public void Run(string serverAddress, int serverPort, string userName, string password,
                string certificateCommonName)
{
    // Create SecureFTPConnection object
    SecureFTPConnection ftpConnection = new SecureFTPConnection();
    
    // setting server address and credentials
    ftpConnection.ServerAddress = serverAddress;
    ftpConnection.ServerPort = serverPort; 
    ftpConnection.UserName = userName;
    ftpConnection.Password = password;
    
    // select explicit FTPS
    ftpConnection.Protocol = FileTransferProtocol.FTPSExplicit;
    
    // select automatic validation of the server's certificate.  The certificate, 
    // or that of the CA that issued it, must be installed into the operating 
    // system's certificate store before this example will work.
    ftpConnection.ServerValidation = SecureFTPServerValidationType.Automatic;
    
    // set the name that we want to match against the server certificate's 
    // Common Name (CN) label
    ftpConnection.ServerCommonName = certificateCommonName;
    
    // show certificate manager to allow user to install the server's
    // certificate or the certificate of the CA that issued it.
    PrintLine("Displaying certificate manager");
    SSLFTPCertificate.ShowCertificateManager("Please ensure that the server's certificate is installed", null);
    
    // connect to server
    PrintLine("Connecting to server " + serverAddress);
    ftpConnection.Connect();
    
    // get the current working directory and files
    PrintLine("Files in directory " + ftpConnection.ServerDirectory + ":");
    PrintLines(ftpConnection.GetFiles());
    
    // Shut down client
    PrintLine("Closing client");
    ftpConnection.Close();
    PrintLine("Example complete");
}
public void Run(string serverAddress, int serverPort, string userName, string password)
{
    // Create SecureFTPConnection object
    SecureFTPConnection ftpConnection = new SecureFTPConnection();
    
    // setting server address and credentials
    ftpConnection.ServerAddress = serverAddress;
    ftpConnection.ServerPort = serverPort; 
    ftpConnection.UserName = userName;
    ftpConnection.Password = password;
    
    // select explicit FTPS
    ftpConnection.Protocol = FileTransferProtocol.FTPSExplicit;
    
    // select automatic validation of the server's certificate, bypassing the
    // CommonName check.  
    ftpConnection.ServerValidation = SecureFTPServerValidationType.AutomaticNoNameCheck;
    
    // The certificate, or that of the CA that issued it, must be installed into 
    // the operating system's certificate store before this example will work,
    // so we show certificate manager to allow user to install the server's
    // certificate or the certificate of the CA that issued it.
    PrintLine("Displaying certificate manager");
    SSLFTPCertificate.ShowCertificateManager("Please ensure that the server's certificate is installed", null);
    
    // connect to server
    PrintLine("Connecting to server " + serverAddress);
    ftpConnection.Connect();
    
    // get the current working directory and files
    PrintLine("Files in directory " + ftpConnection.ServerDirectory + ":");
    PrintLines(ftpConnection.GetFiles());
    
    // Shut down client
    PrintLine("Closing client");
    ftpConnection.Close();
    
    PrintLine("Example complete");
}
public void Run(string serverAddress, int serverPort, string userName, string password,
                string certificateCommonName)
{
    // Create SecureFTPConnection object
    SecureFTPConnection ftpConnection = new SecureFTPConnection();
    
    // setting server address and credentials
    ftpConnection.ServerAddress = serverAddress;
    ftpConnection.ServerPort = serverPort; 
    ftpConnection.UserName = userName;
    ftpConnection.Password = password;
    
    // select explicit FTPS
    ftpConnection.Protocol = FileTransferProtocol.FTPSExplicit;
    
    // select automatic validation of the server's certificate.  The certificate, 
    // or that of the CA that issued it, must be installed into the operating 
    // system's certificate store before this example will work.
    ftpConnection.ServerValidation = SecureFTPServerValidationType.Automatic;
    
    // set the name that we want to match against the server certificate's 
    // Common Name (CN) label
    ftpConnection.ServerCommonName = certificateCommonName;
    
    // prompt for a client certificate and private key
    ftpConnection.ClientCertificate = SSLFTPCertificate.PromptForCertificate(true);
    
    // The server's certificate, or that of the CA that issued it, must be installed into 
    // the operating system's certificate store before this example will work,
    // so we show certificate manager to allow user to install the server's
    // certificate or the certificate of the CA that issued it.
    PrintLine("Displaying certificate manager");
    SSLFTPCertificate.ShowCertificateManager("Please ensure that the server's certificate is installed", null);
    
    // connect to server
    PrintLine("Connecting to server " + serverAddress);
    ftpConnection.Connect();
    
    // get the current working directory and files
    PrintLine("Files in directory " + ftpConnection.ServerDirectory + ":");
    PrintLines(ftpConnection.GetFiles());
    
    // Shut down client
    PrintLine("Closing client");
    ftpConnection.Close();
    
    PrintLine("Example complete");
}
public void Run(string serverAddress, int serverPort, string userName, string password)
{
    // Create SecureFTPConnection object
    SecureFTPConnection ftpConnection = new SecureFTPConnection();
    
    // setting server address and credentials
    ftpConnection.ServerAddress = serverAddress;
    ftpConnection.ServerPort = serverPort;
    ftpConnection.UserName = userName;
    ftpConnection.Password = password;
    
    // select SFTP
    ftpConnection.Protocol = FileTransferProtocol.SFTP;
    
    // turn off server validation
    ftpConnection.ServerValidation = SecureFTPServerValidationType.None;
    
    // connect to server
    PrintLine("Connecting to server " + serverAddress);
    ftpConnection.Connect();
    
    // get the current working directory and files
    PrintLine("Files in directory " + ftpConnection.ServerDirectory + ":");
    PrintLines(ftpConnection.GetFiles());
    
    // Shut down client
    PrintLine("Closing client");
    ftpConnection.Close();
    
    PrintLine("Example complete");
}
public void Run(string serverAddress, int serverPort, string userName, string password, string knownHostsFile)
{
    // Create SecureFTPConnection object
    SecureFTPConnection ftpConnection = new SecureFTPConnection();
    
    // setting server address and credentials
    ftpConnection.ServerAddress = serverAddress;
    ftpConnection.ServerPort = serverPort;
    ftpConnection.UserName = userName;
    ftpConnection.Password = password;
    
    // select SFTP
    ftpConnection.Protocol = FileTransferProtocol.SFTP;
    
    // turn on server validation and set the known-hosts file
    ftpConnection.ServerValidation = SecureFTPServerValidationType.Automatic;
    ftpConnection.KnownHosts.KnownHostsFile = knownHostsFile;
    
    // connect to server
    PrintLine("Connecting to server " + serverAddress);
    ftpConnection.Connect();
    
    // get the current working directory and files
    PrintLine("Files in directory " + ftpConnection.ServerDirectory + ":");
    PrintLines(ftpConnection.GetFiles());
    
    // Shut down client
    PrintLine("Closing client");
    ftpConnection.Close();
    
    PrintLine("Example complete");
}
public void Run(string serverAddress, int serverPort, string userName, string password, string publicKeyFile)
{
    // Create SecureFTPConnection object
    SecureFTPConnection ftpConnection = new SecureFTPConnection();
    
    // setting server address and credentials
    ftpConnection.ServerAddress = serverAddress;
    ftpConnection.ServerPort = serverPort;
    ftpConnection.UserName = userName;
    ftpConnection.Password = password;
    
    // select SFTP
    ftpConnection.Protocol = FileTransferProtocol.SFTP;
    
    // turn on server validation and set the public key for the host
    ftpConnection.ServerValidation = SecureFTPServerValidationType.Automatic;
    ftpConnection.KnownHosts.AddKnownHost(serverAddress, publicKeyFile);
    
    // connect to server
    PrintLine("Connecting to server " + serverAddress);
    ftpConnection.Connect();
    
    // get the current working directory and files
    PrintLine("Files in directory " + ftpConnection.ServerDirectory + ":");
    PrintLines(ftpConnection.GetFiles());
    
    // Shut down client
    PrintLine("Closing client");
    ftpConnection.Close();
    
    PrintLine("Example complete");
}
public void Run(string serverAddress, int serverPort, string userName, string privateKeyFile, string passphrase)
{
    // Create SecureFTPConnection object
    SecureFTPConnection ftpConnection = new SecureFTPConnection();
    
    // setting server address and credentials
    ftpConnection.ServerAddress = serverAddress;
    ftpConnection.ServerPort = serverPort;
    ftpConnection.UserName = userName;
    
    // select SFTP
    ftpConnection.Protocol = FileTransferProtocol.SFTP;
    
    // set the client's private key and the authentication method
    ftpConnection.AuthenticationMethod = AuthenticationType.PublicKey;
    ftpConnection.ClientPrivateKeyFile = privateKeyFile;
    ftpConnection.ClientPrivateKeyPassphrase = passphrase;
    
    // turn off server validation 
    ftpConnection.ServerValidation = SecureFTPServerValidationType.None;
    
    // connect to server
    PrintLine("Connecting to server " + serverAddress);
    ftpConnection.Connect();
    
    // get the current working directory and files
    PrintLine("Files in directory " + ftpConnection.ServerDirectory + ":");
    PrintLines(ftpConnection.GetFiles());
    
    // Shut down client
    PrintLine("Closing client");
    ftpConnection.Close();
    
    PrintLine("Example complete");
}