Alix Reinel
Alix Reinel

Reputation: 145

How to distinguish a directory in SFTP listing retrieved using SharpSSH?

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.

Upvotes: 1

Views: 1169

Answers (1)

Martin Prikryl
Martin Prikryl

Reputation: 202494

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)

Upvotes: 2

Related Questions