Renci.SshNet.SftpClient.UploadFile C# (CSharp) Method

UploadFile() public method

Uploads stream into remote file.
Method calls made by this method to input, may under certain conditions result in exceptions thrown by the stream.
is null. is null or contains only whitespace characters. Client is not connected. Permission to upload the file was denied by the remote host. -or- A SSH command was denied by the server. A SSH error where is the message from the remote host. The method was called after the client was disposed.
public UploadFile ( Stream input, string path, Action uploadCallback = null ) : void
input Stream Data input stream.
path string Remote file path.
uploadCallback Action The upload callback.
return void
        public void UploadFile(Stream input, string path, Action<ulong> uploadCallback = null)
        {
            UploadFile(input, path, true, uploadCallback);
        }

Same methods

SftpClient::UploadFile ( Stream input, string path, bool canOverride, Action uploadCallback = null ) : void

Usage Example

 public bool UploadFile(string filePath)
 {
     ConnectionInfo connectionInfo = new PasswordConnectionInfo(_address, ConstFields.SFTP_PORT, _username, _password);
     try
     {
         using (var sftp = new SftpClient(connectionInfo))
         {
             sftp.Connect();
             using (var file = File.OpenRead(filePath))
             {
                 if (!sftp.Exists(ConstFields.TEMP_PRINT_DIRECTORY))
                 {
                     sftp.CreateDirectory(ConstFields.TEMP_PRINT_DIRECTORY);
                 }
                 sftp.ChangeDirectory(ConstFields.TEMP_PRINT_DIRECTORY);
                 string filename = Path.GetFileName(filePath);
                 sftp.UploadFile(file, filename);
             }
             sftp.Disconnect();
         }
     }
     catch (Renci.SshNet.Common.SshConnectionException)
     {
         Console.WriteLine("Cannot connect to the server.");
         return false;
     }
     catch (System.Net.Sockets.SocketException)
     {
         Console.WriteLine("Unable to establish the socket.");
         return false;
     }
     catch (Renci.SshNet.Common.SshAuthenticationException)
     {
         Console.WriteLine("Authentication of SSH session failed.");
         return false;
     }
     return true;
 }
All Usage Examples Of Renci.SshNet.SftpClient::UploadFile