【发布时间】:2018-02-10 05:25:15
【问题描述】:
有没有办法将文件复制到其他目录中,例如复制/粘贴。
.MoveTo() 方法仅移动 SftpFile,我尝试使用 SftpFile.Attribues.GetBytes() 的 WriteAllBytes() 方法,但它总是写入损坏的文件。
谢谢
【问题讨论】:
标签: c# asp.net .net sftp ssh.net
有没有办法将文件复制到其他目录中,例如复制/粘贴。
.MoveTo() 方法仅移动 SftpFile,我尝试使用 SftpFile.Attribues.GetBytes() 的 WriteAllBytes() 方法,但它总是写入损坏的文件。
谢谢
【问题讨论】:
标签: c# asp.net .net sftp ssh.net
您几乎无法直接复制文件。具体原因见:
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);
}
【讨论】:
以下是如何将远程文件复制到新文件:
using (var sftp = new SftpClient(host, username, password))
{
client.Connect();
using (Stream sourceStream = sftp.OpenRead(remoteFile))
{
sftp.UploadFile(sourceStream, remoteFileNew));
}
}
【讨论】: