Post a reply

Before posting, please read how to report bug or request support effectively.

Bug reports without an attached log file are usually useless.

Options
Add an Attachment

If you do not want to add an Attachment to your Post, please leave the Fields blank.

(maximum 10 MB; please compress large files; only common media, archive, text and programming file formats are allowed)

Options

Topic review

martin

Re: WinSCP How to check error codes in C#

There are no such codes. The SFTP codes won't tell you that the password is wrong. SFTP does not care about passwords.
Check what throws the exception. If Session.Open, then the problem is with connection or authentication (including password). If Session.GetFiles, then the problem is with files.
hohegapo

WinSCP How to check error codes in C#

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.