2014-07-14 189 views
0
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的dll,可以從sftp下載上述文件夾結構嗎?使用SFTP與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); 

} 

logg()是記錄遇到錯誤的函數。

嘗試生成錯誤日誌但沒有記錄。任何想法的人?

+0

你會得到什麼錯誤/例外?會發生什麼,你會發生什麼? – ChrFin

+0

我期待它會遞歸地在我的本地@ChrFin上下載文本文件 – user3836627

+0

該文件夾是否存在本地?任何異常或發生了什麼? – ChrFin

回答

1

思想是使用WinSCP它是相當不錯了一些很好的例子證明...... SharpSSH是非常古老而且我相信不再維護/過時的......

下面是使用的例子...

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); 
       } 
      } 
     } 
    } 
} 
+0

讓我看看它,並會盡快得到回覆 – user3836627

0

如果複製文件及子目錄一樣的scp -r用戶@主持人:/ DIR /上/遠程C:\目錄\上\本地\',你可以這樣做如下。

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(); 
相關問題