Automating the download from sftp server

Advertisement

Guest-Smoroz
Guest

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 https://winscp.net/eng/docs/script_download_most_recent_file
The code is:

using System;
using System.Linq;
using WinSCP;

class Program
{
static int Main(string[] args)
{
try
{
// 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
session.Open(sessionOptions);

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 =
directoryInfo.Files
.Where(file => !file.IsDirectory)
.OrderByDescending(file => file.LastWriteTime)
.FirstOrDefault();

// 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

Advertisement

Advertisement

You can post new topics in this forum