WinSCP 的下载地址: https://winscp.net/eng/downloads.php
下载使用的开发语言EXE.LIB 这里下载.net 下的
这里记录下载文件的时候下载单个文件的坑, 一个目录全部下载可以,但是下载单个文件的时候会出现 not is file 的错误。
这里是因为本地路径没有在最后加 “\\” 引起的。
SessionOptions sessionOptions = new SessionOptions
{
Protocol = Protocol.Sftp,
PortNumber = global.PortNumber,
HostName = global.HostName,
UserName = user,
Password = password,
SshHostKeyFingerprint = global.SshHostKeyFingerprint
};
using (Session session = new Session())
{
// Connect
session.Open(sessionOptions);
TransferOperationResult transferResult;
TransferOptions transferOptions = new TransferOptions();
transferOptions.TransferMode = TransferMode.Binary;
if (!global.Auto)
{
RemoteDirectoryInfo directory = session.ListDirectory(global.NASpath+"/"+global.today+"/");
foreach (RemoteFileInfo fileInfo in directory.Files)
{
if (fileInfo.Name.StartsWith("208"))
{
//Download files 下载208开头的文件,留意第二个参数需要加上一个 "\\" 否则会出现错误无法下载
transferResult = session.GetFiles(global.Serverpath + "/208*", global.directoryFullPath + "\\", false, transferOptions);
//session.GetFileInfo(global.Serverpath + fileInfo.Name)
if (transferOptions != null)
{
transferResult.Check();
}
}
eftForm.AddItem2ListBox(fileInfo.Name);
}
}
if (!File.Exists(global.localPath))
{
Directory.CreateDirectory(global.localPath);
log.WrithLineWithTimeStamp(global.localPath + " Created");
}
//Download files
// transferResult = session.GetFiles(global.Serverpath+"", global.directoryFullPath, false, transferOptions);
// Throw on any error
//transferResult.Check();
session.Abort();