I use WinSCP to download a file from SFTP and this is my code.
SessionOptions sessionOptions = new SessionOptions
{
Protocol = Protocol.Sftp,
HostName = ConfigurationManager.AppSettings["SFTPDomain"],
UserName = ConfigurationManager.AppSettings["SFTPUser"],
Password = ConfigurationManager.AppSettings["SFTPPass"],
GiveUpSecurityAndAcceptAnySshHostKey = true,
PortNumber = 22
};
using (Session session = new Session())
{
//Attempts to connect to your SFTP site
session.Open(sessionOptions);
//Get SFTP File
TransferOptions transferOptions = new TransferOptions();
transferOptions.TransferMode = TransferMode.Binary; //The Transfer Mode - Automatic, Binary, or Ascii
transferOptions.FilePermissions = null; //Permissions applied to remote files;
transferOptions.PreserveTimestamp = false; //Set last write time of destination file
//to that of source file - basically change the timestamp to match destination and source files.
transferOptions.ResumeSupport.State = TransferResumeSupportState.Off;
//SFTP File Path
Sftpserver = ConfigurationManager.AppSettings["SFTPFileName"].ToString();
//Delete File if Exist
if (System.IO.File.Exists(FilePath))
{
System.IO.File.Delete(FilePath);
}
//the parameter list is: remote Path, Local Path with filename
TransferOperationResult transferOperationResult = session.GetFiles("p", FilePath, false, transferOptions);
//Throw on any error
transferOperationResult.Check();
}
How do I check for errors? You have defined the error codes
here. But how can I implement in my code to check if the password is wrong or if the file doesn't come out.