0

I am using 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 can I check the errors. Here they have defined the error codes but how can I implement in my code to check if the password is wrong or file does not exit.

Martin Prikryl
  • 147,050
  • 42
  • 335
  • 704
Coder
  • 169
  • 5
  • 17
  • use a try/catch block.. and try https://sshnet.codeplex.com/ – Mate Oct 07 '14 at 05:43
  • sshnet is a different library i am using WinScp and this is my SFTPClient.cs which has try/catch but error is not going into that try catch but instead in the home try catch when this class is being called – Coder Oct 07 '14 at 05:48
  • Sorry, I confused. This code is the same as for your other question? http://stackoverflow.com/questions/26227676/download-file-using-sshnet – Mate Oct 07 '14 at 05:59

1 Answers1

0

The WinSCP .NET assembly API does not provide error code. Note that WinSCP supports range of protocols, including FTP, SFTP, SCP and WebDAV. So there's no single set of codes to check. Each protocol have different codes. In addition there are errors coming from SSH protocol (what would be a case of wrong password), which are different from SFTP/SCP errors set. Then you have a different set of WinAPI codes for errors when accessing local files.

Martin Prikryl
  • 147,050
  • 42
  • 335
  • 704