4

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());
Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992
Stas Mackarow
  • 175
  • 2
  • 5
  • 16

1 Answers1

5

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

Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992