Topic "Automating the download from sftp server"

Author Message
Guest-Smoroz

Guest


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]
martin
[View user's profile]
Site Admin
Joined: 2002-12-10
Posts: 24555
Location: Prague, Czechia
Show us the exception callstack.
Advertisements

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!

Donate

About donations

$9   $19   $49   $99

About donations

Recommend

WinSCP Privacy Policy

WinSCP License