我正在尝试使用localhost上的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)
派对有点晚,但看起来像一个空的异常可能表示所用的用户帐户没有您要执行的操作所需的权限。