【问题标题】:SSH.NET SftpClient: Copy / Duplicate SftpFileSSH.NET SftpClient:复制/复制 SftpFile
【发布时间】:2018-02-10 05:25:15
【问题描述】:

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

谢谢

【问题讨论】:

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


    【解决方案1】:

    您几乎无法直接复制文件。具体原因见:
    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);
    }
    

    【讨论】:

      【解决方案2】:

      以下是如何将远程文件复制到新文件:

      using (var sftp = new SftpClient(host, username, password))
      {
        client.Connect();
      
        using (Stream sourceStream = sftp.OpenRead(remoteFile))
        {
           sftp.UploadFile(sourceStream, remoteFileNew));
        }
      }
      

      【讨论】:

      • 您的答案在我现有的答案之上显示了什么?
      猜你喜欢
      • 2018-07-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-02-01
      • 2022-01-25
      相关资源
      最近更新 更多