Changing directory with absolute path using Renci.SshNet SftpClient results in SftpPathNotFoundException
The problem was that the string returned from the RunCommand had whitespaces and all I had to do was this:
workingDirectory = getSSHWorkingDirectory.Result.Trim();
I have tried for two days to solve this issue and when I first posted this I got it to work 2 minutes later.
Dan-Levi Tømta
Self-employed software developer from Norway specialising in web based, Windows and Android platform applications. "Information is not knowledge." - Albert Einstein
Updated on June 25, 2022Comments
-
Dan-Levi Tømta almost 2 years
Im working on a simple WPF application that has the sole purpose of using Renci SSH.net library to get the working directory of a SshClient and further passing it to a SftpClient.
I cant seem to change the directory using sftpClient.ChangeDirectory by using the absolute path returned from a RunCommand ("pwd"). I know for certain that the path does exist since the SshClient returns it, but maybe there is something i am doing wrong, or there is a bug? Either way, here is my code:
public static string ssh_host, ssh_username, ssh_password, workingDirectory; public MainWindow() { InitializeComponent(); ssh_host = "XXXXXXXXXX"; ssh_username = "XXXXXXXXXX"; ssh_password = "XXXXXXXXXX"; StartSSH(); } private static void StartSSH() { using (var client = new SshClient(ssh_host, ssh_username, ssh_password)) { try { client.Connect(); if (client.IsConnected) { Console.WriteLine("Client connected"); SshCommand getSSHWorkingDirectory = client.RunCommand("pwd"); workingDirectory = getSSHWorkingDirectory.Result; Console.WriteLine("SSH working directory = " + workingDirectory); // RESULT: SSH working directory = /customers/5/7/9/domain.com/httpd.private using (var sftpClient = new SftpClient(ssh_host, ssh_username, ssh_password)) { sftpClient.Connect(); if (sftpClient.IsConnected) { Console.WriteLine("SFTP working directory = " + sftpClient.WorkingDirectory); // RESULT: SFTP working directory = /customers/5/7/9/domain.com/httpd.www <- NOTE httpd.www sftpClient.ChangeDirectory(workingDirectory); // ERROR: Renci.SshNet.Common.SftpPathNotFoundException: No such file } } } } catch (Exception ex) { Console.WriteLine(ex.Message); } } }
As you can see, there is thrown an exception when trying to change the directory i got using the SshClient RunCommand result.
My question is: Why is ChangeDirectory failing to execute, and how would i forward solving this issue in a proper manner?
Any help is much appreciated.
-
Adrian about 7 yearsThat's often how these things work. Somehow sharing the problem, or even getting someone to stare at your screen gives you some clarity into solving the problem.