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;
}