简体   繁体   中英

WinSCP How to check error codes in C#

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM