C# 使用带有SharpSSH的SFTP下载文件夹和子文件夹
使用Tamir的dll,可以从sftp下载上述文件夹结构吗C# 使用带有SharpSSH的SFTP下载文件夹和子文件夹,c#,directory,sftp,C#,Directory,Sftp,使用Tamir的dll,可以从sftp下载上述文件夹结构吗 sFTP folder structure: MainFolder |_FolderA |_sub1 |_file1.txt |_sub1 |_file2.txt . . . |_sub-n |_filen.txt |_FolderB |_sub1 |_file3.txt |_sub1 |_file4.txt . . . |_sub
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
使用Tamir.Sharpssh;
使用Tamir.Streams;
尝试
{
.
.
.
字符串[]s=Directory.GetFiles(ftpfolder,*.txt,SearchOption.AllDirectories);
对于(int i=0;i
logg()是用于记录遇到的错误的函数
尝试生成错误日志,但未记录任何错误日志。任何人有什么想法吗?我的想法是使用它,它有很好的文档记录和一些很好的例子。。。SharpSSH非常旧,我认为不再维护/过时
下面是一个用法示例
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);
}
如果你像'scp-r'那样递归复制文件和目录user@host:/dir/on/remote C:\dir\on\local\',您可以按如下方式执行此操作
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);
}
}
}
}
}
您会遇到哪些错误/异常?发生了什么?您希望发生什么?我希望它将在我的本地@chrfin上递归下载文本文件该文件夹是否存在于本地?任何异常或发生什么情况?文件夹在本地不存在。我需要将它从sftp下载到我的本地@ChrFinI我想你需要先创建文件夹,然后才能将文件下载到其中…让我检查一下,然后尽快回来谢谢
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();