Post a reply

Before posting, please read how to report bug or request support effectively.

Bug reports without an attached log file are usually useless.

Options
Add an Attachment

If you do not want to add an Attachment to your Post, please leave the Fields blank.

(maximum 10 MB; please compress large files; only common media, archive, text and programming file formats are allowed)

Options

Topic review

martin

Re: Automating the download from sftp server

Show us the exception callstack.
Guest-Smoroz

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]