[英]Download folders and sub-folders using SFTP with SharpSSH
sFTP folder structure:
MainFolder
|_FolderA
|_sub1
|_file1.txt
|_sub1
|_file2.txt
.
.
.
|_sub-n
|_filen.txt
|_FolderB
|_sub1
|_file3.txt
|_sub1
|_file4.txt
.
.
.
|_sub-n
|_filen.txt
Using Tamir's dll, can the above folder structure be downloaded from sftp? 使用Tamir的dll,可以从sftp下载以上文件夹结构吗?
using Tamir.Sharpssh;
using Tamir.Streams;
try
{
.
.
.
string[] s = Directory.GetFiles(ftpfolder,"*.txt", SearchOption.AllDirectories);
for(int i=0; i< s.length; i++)
{
osftp.Get(ftpfolder + s[i], localfolder + Path.GetfileName(s.[i]));
}
}
catch(IOException copyError)
{
logg(copyerror.message);
}
logg() is a function for logging errors encountered. logg()是用于记录遇到的错误的函数。
Tried generating errorlogs but none were logged. 尝试生成错误日志,但未记录任何日志。 Any ideas anyone?
有任何想法吗?
My idea is to use WinSCP it's pretty well documented with some nice examples... SharpSSH is very old and I believe no longer maintained / out of date... 我的想法是使用WinSCP ,并通过一些很好的示例很好地记录了它。SharpSSH很老,我相信它不再维护/已过时...
Here's an example of usage... 这是用法示例...
using System;
using WinSCP;
class Example
{
public static int Main()
{
SessionOptions sessionOptions = new SessionOptions
{
Protocol = Protocol.Sftp,
HostName = EdiConfiguration.FtpIpAddressOrHostName,
UserName = EdiConfiguration.FtpUserName,
Password = EdiConfiguration.FtpPassword,
SshHostKeyFingerprint = EdiConfiguration.SshHostKeyFingerprint,
PortNumber = EdiConfiguration.FtpPortNumber
};
using (Session session = new Session())
{
session.Open(sessionOptions);
TransferOptions transferOptions = new TransferOptions();
transferOptions.TransferMode = TransferMode.Binary;
transferOptions.ResumeSupport.State = TransferResumeSupportState.Off;
// Download the files in the OUT directory.
TransferOperationResult transferOperationResult = session.GetFiles(EdiConfiguration.FtpDirectory, EdiConfiguration.IncommingFilePath, false, transferOptions);
// Check and throw if there are any errors with the transfer operation.
transferOperationResult.Check();
// Remove files that have been downloaded.
foreach (TransferEventArgs transfer in transferOperationResult.Transfers)
{
RemovalOperationResult removalResult = session.RemoveFiles(session.EscapeFileMask(transfer.FileName));
if (!removalResult.IsSuccess)
{
eventLogUtility.WriteToEventLog("There was an error removing the file: " + transfer.FileName + " from " + sessionOptions.HostName + ".", EventLogEntryType.Error);
}
}
}
}
}
If you copy files and directories recursively like 'scp -r user@host:/dir/on/remote C:\\dir\\on\\local\\', you can do this as follows. 如果递归地复制文件和目录,例如“ scp -r user @ host:/ dir / on / remote C:\\ dir \\ on \\ local \\”,则可以执行以下操作。
using Tamir.SharpSsh;
var host = "host address";
var user = @"user account";
var password = @"user password";
var scp = new Scp( host, user, password );
scp.Connect();
scp.Recursive = true;
var remotePath = @"/dir/on/remote";
var localPath = @"C:\dir\on\local\";
scp.Get( remotePath, localPath );
scp.Close();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.