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.
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:
SftpClient.ListDirectory
returning the IEnumerable<SftpFile>
. The SftpFile
has the .IsDirectory
propertySession.ListDirectory
returning (via the RemoteDirectoryInfo.Files
) a collection of the RemoteFileInfo
with property .IsDirectory