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

时间:2014-10-07 05:41:07

标签: c# .net sftp winscp winscp-net

我正在使用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();
}

如何检查错误。 Here他们已经定义了错误代码,但我如何在我的代码中实现检查密码是否错误或文件是否退出。

1 个答案:

答案 0 :(得分:0)

WinSCP .NET程序集API不提供错误代码。请注意,WinSCP支持各种协议,包括FTP,SFTP,SCP和WebDAV。因此,没有一套代码需要检查。每个协议都有不同的代码。此外,还存在来自SSH协议的错误(密码错误的情况),这与SFTP / SCP错误集不同。然后,您在访问本地文件时会遇到一组不同的WinAPI代码。