C#SSHNet失败,在阅读和使用SftpClient

问题描述:

我试图读取SftpClient /写文本在localhost:C#SSHNet失败,在阅读和使用SftpClient

 try 
     { 
      AuthenticationMethod authenticationMethod = new PasswordAuthenticationMethod("user", "password"); 
      ConnectionInfo connection = new ConnectionInfo("localhost", "user", authenticationMethod); 
      using (var client = new SftpClient(connection)) 
      { 
       client.Connect(); 

       //Upload File 
       using(var stream = new FileStream("C:\\Users\\MY-PC\\file1.txt",FileMode.Open)) 
       { 
        client.UploadFile(stream,"/C/Users/MY-PC/sftp-folder/file1-copy.txt"); 
       } 

       //Create and Write on a new file 
       client.WriteAllText("/C/Users/MY-PC/sftp-folder/file2.txt", "hello world"); 

       //Read from created file 
       string text = client.ReadAllText("/C/Users/MY-PC/sftp-folder/file1-copy.txt"); 
      } 
      } 
     catch (Exception e) 
     { 
      throw(e); 
     } 

第一种方法(UploadFile)的作品,并正确地创建该文件。第二种方法(WriteAllText)创建该文件,但不写任何内容,并抛出Renci.SshNet.Common.SshException。这里的堆栈跟踪:

 in Renci.SshNet.Sftp.SftpSession.RequestFStat(Byte[] handle) 
    in Renci.SshNet.Sftp.SftpFileStream..ctor(ISftpSession session, String path, FileMode mode, FileAccess access, Int32 bufferSize, Boolean useAsync) 
    in Renci.SshNet.Sftp.SftpFileStream..ctor(ISftpSession session, String path, FileMode mode, FileAccess access, Int32 bufferSize) 
    in Renci.SshNet.SftpClient.OpenWrite(String path) 
    in Renci.SshNet.SftpClient.CreateText(String path, Encoding encoding) 
    in Renci.SshNet.SftpClient.WriteAllText(String path, String contents, Encoding encoding) 
    in myMethod in myClass: line 991 //Abbreviated for simplicity 
    in Renci.SshNet.Sftp.SftpFileStream..ctor(ISftpSession session, String path, FileMode mode, FileAccess access, Int32 bufferSize, Boolean useAsync) 
    in Renci.SshNet.Sftp.SftpFileStream..ctor(ISftpSession session, String path, FileMode mode, FileAccess access, Int32 bufferSize) 
    in Renci.SshNet.SftpClient.OpenWrite(String path) 
    in Renci.SshNet.SftpClient.CreateText(String path, Encoding encoding) 
    in Renci.SshNet.SftpClient.WriteAllText(String path, String contents, Encoding encoding) 
    in myMethod in myClass: line 991 //Abbreviated for simplicity 

同样的事情将发生在第三个方法(ReadAllText)。流似乎存在问题,但我不明白为什么。与远程连接相同的代码完美地工作,所以我想我的电脑可能是问题。我查了防火墙,但它似乎没事(否则我无法连接,我猜)。每一个建议将不胜感激。

+0

这个例外是什么意思?为什么你称之为“本地路径”,何时是远程路径?向我们展示一些实际路径。 –

+0

@MartinPrikryl它不是一个远程路径。我将我的应用程序连接到localhost,并在本地上传/写入/读取,这是因为我称之为“localPath”。我知道这可能没有道理,但我当然有我的理由。考虑到例外情况,该消息是一个空字符串 –

+0

仍然是远程路径,因为它在服务器上。我们不在乎它是本地服务器。你让你的问题困扰读者。 –

晚会有点晚,但它看起来像一个空的异常可以表示使用的用户帐户不具备您所要做的所需权限。