public bool SendSFTPFile(Enums.SFTP_Site _SFTP_Site, string _InPath, string _SFTPPath) { bool retVal = false; if (!File.Exists(_InPath)) { return(false); } try { Logger.Trace("TRANSFERRING FILE TO SFTP LOCATION: "); using (SftpClient sftpClient = GetSFTPClient(_SFTP_Site)) { Logger.Trace("SFTP Connection established... "); sftpClient.Connect(); //sftp.ChangeDirectory("/MyFolder"); using (var uplfileStream = File.OpenRead(_InPath)) { sftpClient.UploadFile(uplfileStream, _SFTPPath, true); Logger.Trace("SFTP file stream uploaded..."); } sftpClient.Disconnect(); retVal = true; } } catch (Exception ex) { Logger.Error(ex, "Error While SFTP File Transfer"); } return(retVal); }
private SftpClient GetSFTPClient(Enums.SFTP_Site _SFTP_Site) { SftpClient sftpClient = null; string SFTP_Host = string.Empty; int SFTP_Port = 0; string SFTP_Usr = string.Empty; string SFTP_Pwd = string.Empty; switch (_SFTP_Site) { case Enums.SFTP_Site.Default: break; case Enums.SFTP_Site.Input: SFTP_Host = ConfigHelper.Model.InputFilePathLocation_SFTP_Host; SFTP_Port = ConfigHelper.Model.InputFilePathLocation_SFTP_Port; SFTP_Usr = ConfigHelper.Model.InputFilePathLocation_SFTP_Usr; SFTP_Pwd = ConfigHelper.Model.InputFilePathLocation_SFTP_Pwd; break; case Enums.SFTP_Site.Output: SFTP_Host = ConfigHelper.Model.OutputFilePathLocation_SFTP_Host; SFTP_Port = ConfigHelper.Model.OutputFilePathLocation_SFTP_Port; SFTP_Usr = ConfigHelper.Model.OutputFilePathLocation_SFTP_Usr; SFTP_Pwd = ConfigHelper.Model.OutputFilePathLocation_SFTP_Pwd; break; default: break; } if (!string.IsNullOrEmpty(SFTP_Host) && SFTP_Port > 0 && !string.IsNullOrEmpty(SFTP_Usr) && !string.IsNullOrEmpty(SFTP_Pwd)) { sftpClient = new SftpClient(SFTP_Host, SFTP_Port, SFTP_Usr, SFTP_Pwd); } return(sftpClient); }