1

I need to recursively download files from the server to SFTP by SharpSsh. But I do not understand how to determine that the file name or the directory. Now I do so

 static void recDir(string remotePath, string localPath)
 {
     Sftp _c = this.sftp;
     ArrayList FileList = _c.GetFileList(remotePath);
     FileList.Remove(".");
     FileList.Remove("..");
     for (int i = 0; i < FileList.Count; i++)
     {
         try
         {
             _c.Get(remotePath + "/" + FileList[i], localPath + "/" + FileList[i]);

             Console.WriteLine("File: " + remotePath + "/" + FileList[i]);
         }
         catch (Exception e)
         {
             Console.WriteLine("Dir: " + remotePath + "/" + FileList[i]);
                System.IO.Directory.CreateDirectory(localPath + "/" + FileList[i]);
                recDir(remotePath + "/" + FileList[i], localPath + "/" + FileList[i]);
         }
     }
 }

It works, but it seems not correct.

Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992
Alix Reinel
  • 145
  • 10
  • your code seems like when any exception occurred then you tried to recursion not clear where you want recursion.. – Dhaval Patel Mar 09 '15 at 07:42
  • No idea what this means: `It works, but it seems not correct.` – Sam Axe Mar 09 '15 at 07:55
  • @Dan-o I'm quite sure it means that the OP is trying to make robust code, not code that "just happen to work". Using exceptions for regular program flow is smelly. – C.Evenhuis Mar 09 '15 at 08:01

1 Answers1

2

SharpSSH API does not allow that. But you can code it as SharpSSH is open source. Note that SharpSSH is a poor choice though. It's not maintained for years.


See my answers to another similar SharpSSH questions:

Or use another SFTP library:

  • SSH.NET has the method SftpClient.ListDirectory returning the IEnumerable<SftpFile>. The SftpFile has the .IsDirectory property
  • WinSCP .NET assembly has the method Session.ListDirectory returning (via the RemoteDirectoryInfo.Files) a collection of the RemoteFileInfo with property .IsDirectory
    (I'm the author of WinSCP .NET assembly)
Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992