Search code examples
c#.netftpwinscpwinscp-net

Copy file on remote server using WinSCP .NET Assembly


I have been using WinSCP .NET Assembly in one of my projects to transfer files from one FTP server to another. Now, I want to copy a file from one directory to another (both present on the same FTP server). I have found the following method in WinSCP .NET assembly:

MoveFile(string sourcePath, string targetPath)

But it moves the file from source directory to target path (Deletes from source path) whereas I want to keep a copy on both paths.

I have found out that there is a method called duplicate in WinSCP client that performs the copy operation but it is not available in assembly.

Is there any way to retain the original file at source path and create a copy at destination as well?

Note:

My question is specific to WinSCP .Net assembly. I am not asking about FTP protocol.


Solution

  • Use Session.DuplicateFile method:

    public void DuplicateFile(string sourcePath, string targetPath)
    

    Though note that it works only if the server actually supports the duplication, what most servers don't.

    While the GUI "Duplicate" feature will fall back to "download and upload" (called "Duplicate via local temporary copy" in GUI). In .NET assembly, you can do the same using Session.GetFiles and Session.PutFiles, Session.DuplicateFile won't do it for you automatically.