WinSCP如何在C#中检查错误代码

本文关键字:检查 错误代码 WinSCP | 更新日期: 2023-09-27 18:25:42

我正在使用WinSCP从SFTP下载一个文件,这是我的代码。

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();
}

我该如何检查错误。在这里,他们定义了错误代码,但我如何在代码中实现,以检查密码是否错误或文件是否未退出。

WinSCP如何在C#中检查错误代码

WinSCP.NET程序集API不提供错误代码。请注意,WinSCP支持一系列协议,包括FTP、SFTP、SCP和WebDAV。所以没有一组代码需要检查。每个协议都有不同的代码。此外,还有来自SSH协议的错误(密码错误的情况),这与设置的SFTP/SCP错误不同。然后,您就有了一组不同的WinAPI代码,用于访问本地文件时的错误。