Hi
I would like to be able to keep the connection to the FTP server after downloading the file, but it keep logging in and disconnecting after each download.
The code:
using Renci.SshNet;
using Renci.SshNet.Sftp;
static void SftpDownload()
{
string host = @"sftp.server";
string username = "username";
string password = @"password";
string localpath = "C:/Temp/";
// Path to file on SFTP server
string pathRemoteFile = "/data.txt";
// Path where the file should be saved once downloaded (locally)
System.IO.Directory.CreateDirectory(localpath);
string pathLocalFile = (localpath + "/data.txt");
using (SftpClient sftp = new SftpClient(host, username, password))
{
try
{
while (Program.ftpcon == true)
{
sftp.Connect();
Program.ftpcon = false;
}
using (Stream fileStream = File.OpenWrite(pathLocalFile))
{
sftp.DownloadFile(pathRemoteFile, fileStream);
}
// Disabled due to heavy load on FTP server
//sftp.Disconnect();
}
catch (Exception er)
{
Console.WriteLine("An exception has been caught " + er.ToString());
}
}
}
(ftpcon is defined outside)
Any help is greatly appriciated!