PowerTCP SSH and SFTP for .NET
Put(String,String,CopyMode) Method
Example 




An absolute or relative (from the current working directory) path specifying the file on the client.
An absolute or relative (from WorkingDirectory) path specifying the file on the server. Use "/" as the directory delimiter.
Specifies the type of copy to perform.
Copies a file from the local client to the remote server.
Syntax
'Declaration
 
Public Overloads Function Put( _
   ByVal localFileName As String, _
   ByVal remoteFileName As String, _
   ByVal mode As CopyMode _
) As CopyResult
'Usage
 
Dim instance As Sftp
Dim localFileName As String
Dim remoteFileName As String
Dim mode As CopyMode
Dim value As CopyResult
 
value = instance.Put(localFileName, remoteFileName, mode)
public CopyResult Put( 
   string localFileName,
   string remoteFileName,
   CopyMode mode
)
public: CopyResult* Put( 
   string* localFileName,
   string* remoteFileName,
   CopyMode mode
) 
public:
CopyResult^ Put( 
   String^ localFileName,
   String^ remoteFileName,
   CopyMode mode
) 

Parameters

localFileName
An absolute or relative (from the current working directory) path specifying the file on the client.
remoteFileName
An absolute or relative (from WorkingDirectory) path specifying the file on the server. Use "/" as the directory delimiter.
mode
Specifies the type of copy to perform.
Remarks

If remoteFileName includes a path or part of a path that must be created on the server, Put(String,String,Boolean,Boolean,CopyMode) may be used to automatically create the folder structure on the server.

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, or the Error event is raised if an exception is thrown.

Example
The example below demonstrates how to Put a file.
private void button1_Click(object sender, System.EventArgs e)
{
    //Connect and log into the server
    connectAndLogin(myServerHostname, myServerPort, myUsername, myPassword);
    //Upload the file
    sftp1.Put(localFilepath, remoteFilename, 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)
    'Upload the file
    sftp1.Put(localFilepath, remoteFilename, 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