SSH.NET SftpClient:复制/复制SftpFile

时间:2017-09-01 07:30:00

标签: c# asp.net .net sftp ssh.net

有没有办法将文件复制到其他目录中,如复制/粘贴。 .MoveTo()方法仅移动SftpFile,我使用WriteAllBytes()尝试SftpFile.Attribues.GetBytes()方法,但它始终写入损坏的文件。

谢谢

2 个答案:

答案 0 :(得分:0)

您几乎无法直接复制文件。详情请见:
In an SFTP session is it possible to copy one remote file to another location on same remote SFTP server?

所以你必须下载并重新上传文件。

最简单的方法(不创建临时本地文件)是:

SftpClient client = new SftpClient("exampl.com", "username", "password");
client.Connect();

using (Stream sourceStream = client.OpenRead("/source/path/file.dat"))
using (Stream destStream = client.Create("/dest/path/file.dat"))
{
    sourceStream.CopyTo(destStream);
}

答案 1 :(得分:-1)

以下是将远程文件复制到新文件的方法:

using (var sftp = new SftpClient(host, username, password))
{
  client.Connect();

  using (Stream sourceStream = sftp.OpenRead(remoteFile))
  {
     sftp.UploadFile(sourceStream, remoteFileNew));
  }
}