Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to download a single file using Session.GetFiles in WinSCP C#

So, basically I am trying to download only one file from the entire folder on my server.

Folder "domain" contains those files right now:

File1.txt
File2.txt
File3.txt

So, as I can see in WinSCP docs if I want to download only one file I still should use Session.GetFiles() (method docs) with full path to file. Okay, but I can't understand what's my problem, because it's not working.

session.GetFiles("/domains/domain/File1.txt", Directory.GetCurrentDirectory());

However if I change the remote path to whole directory, not a single file I need it works great, but I don't need all three .txt files.

session.GetFiles("/domains/domain/", Directory.GetCurrentDirectory());
like image 808
Stas Mackarow Avatar asked Oct 31 '25 03:10

Stas Mackarow


1 Answers

As the documentation for the localPath argument of Session.GetFiles says, the argument is:

Full path to download the file to.

So it should be:

var localPath = Path.Combine(Directory.GetCurrentDirectory(), "File1.txt");
session.GetFiles("/domains/domain/File1.txt", localPath);

Alternatively, you can simplify the code by using Session.GetFileToDirectory, which does what you have expected from Session.GetFiles:

session.GetFileToDirectory(
    "/domains/domain/File1.txt", Directory.GetCurrentDirectory());

See also https://winscp.net/eng/docs/faq_script_vs_gui#inputs

like image 63
Martin Prikryl Avatar answered Nov 02 '25 18:11

Martin Prikryl



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!