Topic "Automating the download from sftp server"

Author Message


I am trying to automate the download of the file from sftp server.
I am using the C# code sample from
The code is:

using System;
using System.Linq;
using WinSCP;

class Program
static int Main(string[] args)
// Setup session options
SessionOptions sessionOptions = new SessionOptions
Protocol = Protocol.Sftp,
HostName = "xxxxxx",
UserName = "xxxxxx",
Password = "xxxxxx",
PortNumber = 60022,
SshHostKeyFingerprint = "ssh-rsa 2048 xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx",

using (Session session = new Session())
// Connect

const string remotePath = "/GOOGLENMC5/BULKTRK/";
const string localPath = "c:\\FEDEXNMC\\BULKTRK\\";

// Get list of files in the directory
RemoteDirectoryInfo directoryInfo = session.ListDirectory(remotePath);

// Select the most recent file
RemoteFileInfo latest =
.Where(file => !file.IsDirectory)
.OrderByDescending(file => file.LastWriteTime)

// Any file at all?
if (latest == null)
throw new Exception("No file found");

// Download the selected file
session.GetFiles(session.EscapeFileMask(remotePath + latest.Name), localPath).Check();

return 0;
catch (Exception e)
Console.WriteLine("Error: {0}", e);
return 1;

When I build and run the code I am getting:

The thread 0x16a8 has exited with code 0 (0x0).
Exception thrown: 'System.ArgumentException' in WinSCPnet.dll

Any suggestions?[/quote]
[View user's profile]
Site Admin
Joined: 2002-12-10
Posts: 25310
Location: Prague, Czechia
Show us the exception callstack.

You can post new topics in this forum

Search Site

What is WinSCP?

It is award-winning SFTP client, SCP client, FTPS client and FTP client integrated into one software program for file transfer to FTP server or secure SFTP server. [More]

And it's free!


About donations

$9   $19   $49   $99

About donations


WinSCP Privacy Policy

WinSCP License