简体   繁体   English

使用带有SharpSSH的SFTP下载文件夹和子文件夹

[英]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.

相关问题 如何计算收件箱子文件夹,包括Outlook中子文件夹下的子文件夹等 - How to count inbox sub-folders including sub-folders under sub-folders and so on in Outlook 如何使用 EWS 托管 API 从文件夹和公共文件夹的子文件夹中获取所有项目 - How To Get all ITEMS from Folders and Sub-folders of PublicFolders Using EWS Managed API 复制文件夹但忽略一些子文件夹 - Copy a folder but ignoring some sub-folders 从 Dropbox 上的公共共享文件夹和子文件夹下载图像 - Downloading images from publicly shared folders and sub-folders on Dropbox 使用Tamir.SharpSSH&#39;no such file&#39;通过sftp下载文件 - 'No such file' using Tamir.SharpSSH to download file via sftp 为C#Intellisense生成XML文件,为子文件夹损坏 - Generating XML file for C# Intellisense, broken for sub-folders NUnit测试用例生成:如何创建子文件夹(层次结构)? - NUnit testcase generation: how to create sub-folders (hierarchy)? 从特定结构的子文件夹中获取文件? - Get files from specifically structured sub-folders? 忽略特定子文件夹的 C# 编译警告? - Ignore C# compile warnings for specific sub-folders? 从 C# 中的文件夹和子文件夹中获取文件名 - Get file names from folder and sub-folders in C#
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM