WinSCP How to check error codes in C#

Advertisement

hohegapo
Joined:
Posts:
1
Location:
Sabetha

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.

Reply with quote

Advertisement

martin
Site Admin
martin avatar
Joined:
Posts:
40,632
Location:
Prague, Czechia

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.

Reply with quote

Advertisement

You can post new topics in this forum