PowerTCP SSH and SFTP for .NET
Get(String,String,Boolean,Boolean,CopyMode,Object) Method
Example 




An absolute or relative (from WorkingDirectory) path specifying the search pattern on the server. Must contain a wildcard (*).
An absolute or relative (from the current working directory) path specifying the destination root directory on the client.
Specify true to search sub-directories resursively.
Specify true to include empty folders in the results.
Specifies the type of copy to perform.
An object that can be used to identify this copy operation when the progress event is raised during multiple concurrent copy operations.
Sequentially copies multiple files from the remote server to the local client.
Syntax
'Declaration
 
Public Overloads Function Get( _
   ByVal remoteSearchPattern As String, _
   ByVal localDirectory As String, _
   ByVal recurse As Boolean, _
   ByVal includeEmptyFolders As Boolean, _
   ByVal mode As CopyMode, _
   ByVal userToken As Object _
) As CopyResult()
'Usage
 
Dim instance As Sftp
Dim remoteSearchPattern As String
Dim localDirectory As String
Dim recurse As Boolean
Dim includeEmptyFolders As Boolean
Dim mode As CopyMode
Dim userToken As Object
Dim value() As CopyResult
 
value = instance.Get(remoteSearchPattern, localDirectory, recurse, includeEmptyFolders, mode, userToken)

Parameters

remoteSearchPattern
An absolute or relative (from WorkingDirectory) path specifying the search pattern on the server. Must contain a wildcard (*).
localDirectory
An absolute or relative (from the current working directory) path specifying the destination root directory on the client.
recurse
Specify true to search sub-directories resursively.
includeEmptyFolders
Specify true to include empty folders in the results.
mode
Specifies the type of copy to perform.
userToken
An object that can be used to identify this copy operation when the progress event is raised during multiple concurrent copy operations.
Exceptions
ExceptionDescription
System.ArgumentExceptionThrown if the remoteSearchPattern does not contain a wildcard character (*).
Remarks

This method will only raise an exception if the remoteSearchPattern is invalid. For exceptions that occurred during execution, check CopyResult.GetException() of each returned CopyResult.

The Progress event is raised as this method executes if an event handler is attached.

If Sftp.ThreadingModel is ThreadingModel.ApartmentAsync, this method immediately returns a null value after starting a worker thread that executes the operation asynchronously. The Progress event is raised containing the result upon completion.

Example
The example below demonstrates how to Get all files and directories under a directory.
private void button1_Click(object sender, System.EventArgs e)
{
    //Connect and log into the server
    connectAndLogin(myServerHostname, myServerPort, myUsername, myPassword);
    //Download files that match the search pattern
    //If remoteSearchPattern is "MyFiles/*" it will download all files and folders under 'MyFiles'
    CopyResult[] copyResults = sftp1.Get(remoteSearchPattern, localSaveDir, true, true, CopyMode.Copy);
    //Logout and close the connection.
    sftp1.Close();
}

private void connectAndLogin(string hostNameOrAddress, int port, string username, string password)
{
    //Connect to the server
    sftp1.Connection.RemoteEndPoint.HostNameOrAddress = hostNameOrAddress;
    sftp1.Connection.RemoteEndPoint.Port = port; //Usually 22
    sftp1.Connect();

    //Authenticate
    SshLoginData loginDetails = new SshLoginData();
    loginDetails.Username = username;
    loginDetails.Password = password;
    sftp1.Authenticate(loginDetails);
}
Private Sub button1_Click(ByVal sender As Object, ByVal e As System.EventArgs)
    'Connect and log into the server
    connectAndLogin(myServerHostname, myServerPort, myUsername, myPassword)
    'Download files that match the search pattern
    'If remoteSearchPattern is "MyFiles/*" it will download all files and folders under 'MyFiles'
    Dim copyResults() As CopyResult = sftp1.Get(remoteSearchPattern, localSaveDir, True, True, CopyMode.Copy)
    'Logout and close the connection.
    sftp1.Close()
End Sub

Private Sub connectAndLogin(ByVal hostNameOrAddress As String, ByVal port As Integer, ByVal username As String, ByVal password As String)
    'Connect to the server
    sftp1.Connection.RemoteEndPoint.HostNameOrAddress = hostNameOrAddress
    sftp1.Connection.RemoteEndPoint.Port = port 'Usually 22
    sftp1.Connect()

    'Authenticate
    Dim loginDetails As New SshLoginData()
    loginDetails.Username = username
    loginDetails.Password = password
    sftp1.Authenticate(loginDetails)
End Sub
See Also

Reference

Sftp Class
Sftp Members
Overload List


PowerTCP SSH and SFTP for .NET Documentation Version 7.0
© 2023 Dart Communications. All Rights Reserved.
Send comments on this topic