MGet Code Example

The following example demonstrates an SFTP session in which multiple files are downloaded from the server. In this example, all text files in a directory (including sub-directories) are retrieved.


private void mGet_Click(object sender, System.EventArgs e)
    //Store session info in an array to pass to the method
    //executing on the worker thread.
    string[] sessionInfo = { "mySftpServer""myUsername""myPassword",
                                "MyFiles/*""c:\\MyFiles" };

    //This application has a User-Interface, which we do not want to block.
    //Start launches the specified method on a worker thread.
    //Event handlers execute on the UI thread.
    sftp1.Start(mget, sessionInfo);

private void mget(object state)
    //This function performs an SFTP MGet operation using blocking calls.
    //Use the Start method to execute this function on a worker thread.
    //Session info is passed in as a 5-length string array.
    string[] sessionInfo = (string[])state;
    string host = sessionInfo[0];
    string username = sessionInfo[1];
    string password = sessionInfo[2];
    string remotePath = sessionInfo[3];
    string localPath = sessionInfo[4];

    //Connect and Authenticate.

    sftp1.Connection.RemoteEndPoint.HostNameOrAddress = host;
    sftp1.Authenticate(username, password);

    //Get recursive list of folders and files, including empty folders.
    CopyResult[] files = sftp1.Get(remotePath, localPath, truetrueCopyMode.Copy);