【发布时间】:2014-10-07 05:41:06
【问题描述】:
我正在使用 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 他们已经定义了错误代码,但是我如何在我的代码中实现检查密码是否错误或文件不退出。
【问题讨论】:
-
使用 try/catch 块.. 并尝试sshnet.codeplex.com
-
sshnet 是一个不同的库,我正在使用 WinScp,这是我的 SFTPClient.cs,它具有 try/catch,但错误不会进入该 try catch,而是在这个类正在执行时出现在 home try catch叫
-
对不起,我弄糊涂了。此代码与您的其他问题相同吗? stackoverflow.com/questions/26227676/download-file-using-sshnet
标签: c# .net sftp winscp winscp-net