2017-09-01 77 views
1

有沒有辦法在複製/粘貼等複製其他目錄中的文件。 .MoveTo()方法只移動SftpFile,我試過WriteAllBytes()方法使用SftpFile.Attribues.GetBytes(),但它總是寫入一個損壞的文件。SSH.NET SftpClient:複製/複製SftpFile

謝謝

回答

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

這裏是如何複製遠程文件到新的一個:

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

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

什麼是你的答案顯示在我現有的答案的頂部? –