Automating the download from sftp server



Automating the download from sftp server

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]

Reply with quote



You can post new topics in this forum