2015-07-19 125 views
2

进出口工作在具有使用Renci SSH.net库获得SshClient的工作目录,并进一步将其传递到SftpClient的唯一目的,一个简单的WPF应用程序。更改目录

我似乎无法通过使用来自RunCommand(“PWD”)返回的绝对路径更改使用sftpClient.ChangeDirectory目录。我知道确切的道路,因为SshClient返回它,但也许有一些我做错了,或者有一个错误?无论哪种方式,这里是我的代码:

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); 
      } 
     } 
    } 

正如你所看到的,还有正在试图改变我得到了使用SshClient RunCommand结果目录时抛出的异常。

我的问题是:为什么ChangeDirectory未能执行,以及如何将我推进以适当的方式解决这一问题?

任何帮助,非常感谢。

回答

1

的问题是返回的字符串从RunCommand有空格和所有我需要做的是这样的:

workingDirectory = getSSHWorkingDirectory.Result.Trim(); 

我已经试过了两天来解决这个问题,当我第一次公布这个我它在2分钟后工作。

+0

这通常是这些事情的工作方式。以某种方式分享问题,或者让某人盯着你的屏幕给你一些清晰的解决问题。 – Adrian