1

Is there a way to copy a file in an other directory like a copy/paste. .MoveTo() method move only the SftpFile and I tried WriteAllBytes() method using SftpFile.Attribues.GetBytes(), but it writes always a corrupted file.

Thank you

Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992
aMASKed
  • 11
  • 1
  • 2

2 Answers2

0

You will hardly be able to copy the file directly. For details why, see:
In an SFTP session is it possible to copy one remote file to another location on same remote SFTP server?


So you have to download and re-upload the file.

The easiest way to do that (without creating a temporary local file) is:

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);
}
Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992
-1

Here is how to copy remote file to new one:

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

  using (Stream sourceStream = sftp.OpenRead(remoteFile))
  {
     sftp.UploadFile(sourceStream, remoteFileNew));
  }
}
Aleks Andreev
  • 7,016
  • 8
  • 29
  • 37
Yurii
  • 1