2014-10-07 5 views
0

WinSCP를 사용하여 SFTP에서 파일을 다운로드하고 있는데 이것은 내 코드입니다.WinSCP C#에서 오류 코드를 확인하는 방법

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 그들은 오류 코드를 정의했으나 암호가 잘못되었거나 파일이 종료되지 않았는지 확인하기 위해 어떻게 코드에 구현할 수 있습니까?

+0

try/catch 블록을 사용하고 https://sshnet.codeplex.com/을 시도하십시오. – Mate

+0

sshnet은 WinScp를 사용하고있는 다른 라이브러리입니다.이 태그는 try/catch하지만 오류가 발생하지 않습니다 내 SFTPClient.cs입니다. 그 집에 캐치하려고하지만 집에서 대신이 클래스가 호출 될 때 잡으려고 – Coder

+0

미안하지만, 혼란 스러웠습니다. 이 코드는 다른 질문과 동일합니다. http://stackoverflow.com/questions/26227676/download-file-using-sshnet – Mate

답변

0

WinSCP .NET 어셈블리 API는 오류 코드를 제공하지 않습니다. WinSCP는 FTP, SFTP, SCP 및 WebDAV를 포함한 다양한 프로토콜을 지원합니다. 확인할 코드는 하나도 없습니다. 각 프로토콜에는 서로 다른 코드가 있습니다. 또한 SFTP/SCP 오류 설정과 다른 SSH 프로토콜 (잘못된 암호의 경우)에서 오는 오류가 있습니다. 그런 다음 로컬 파일에 액세스 할 때 오류에 대한 다른 WinAPI 코드 집합이 있습니다.