The following does not list files in the /home directory, but rather lists files in the / (root) directory:
sftp.ChangeDirectory("home");
sftp.ListDirectory("").Select (s => s.FullName);
The following doesn't work and returns SftpPathNotFoundException:
sftp.ChangeDirectory("home");
sftp.ListDirectory("home").Select (s => s.FullName);
Following is the correct way to list files in /home directory
sftp.ChangeDirectory("/");
sftp.ListDirectory("home").Select (s => s.FullName);
get recursively
void Main()
{
using (var client = new Renci.SshNet.SftpClient("sftp.host.com", "user", "password"))
{
var files = new List<String>();
client.Connect();
ListDirectory(client, ".", ref files);
client.Disconnect();
files.Dump();
}
}
void ListDirectory(SftpClient client, String dirName, ref List<String> files)
{
foreach (var entry in client.ListDirectory(dirName))
{
if (entry.Name == "." || entry.Name == "..")
continue;
if (entry.IsDirectory)
{
ListDirectory(client, entry.FullName, ref files);
}
else
{
files.Add(entry.FullName);
}
}
}