public void ConnectSFTP()
{
// Set up session options
SessionOptions sessionOptions = new SessionOptions
{
Protocol = Protocol.Sftp,
HostName = SFTP_Host,
PortNumber = SFTP_Port,
UserName = SFTP_UserID,
Password = SFTP_Password,
SshHostKeyFingerprint = SFTP_SshHostKeyFingerprint
};
sessionOptions.AddRawSettings("Cipher", "aes,chacha20,3des,WARN,des,blowfish,arcfour");
session = new Session();
session.ExecutablePath = ExePath;
// Connect
try
{
session.Open(sessionOptions);
TraceLog.appendTransactionLog("Connected to " + SFTP_Host + ":" + SFTP_Port);
}
catch (Exception ex)
{
TraceLog.appendTransactionLog("Failed to connect to " + SFTP_Host + ":" + SFTP_Port + " (" + SFTP_SshHostKeyFingerprint + ")" + Environment.NewLine + "Error: " + ex.Message);
}
}
public FileDownloadObj DownloadFiles(DateTime dt)
{
....
ConnectSFTP();
if (session.Opened)
{
using (session)
{
sssion.QueryReceived += (sender, e) =>
{
sLogText = string.Format("Error: {0}{1}", e.Message, Environment.NewLine);
TraceLog.appendTransactionLog(sLogText);
sErrMsg += string.Format("Error: {0}{1}", sLogText, Environment.NewLine); ;
e.Continue();
};
// Transfer options
TransferOptions oTrRes = new TransferOptions();
oTrRes.TransferMode = TransferMode.Automatic; //The Transfer Mode - Automatic, Binary, or Ascii
oTrRes.FilePermissions = null; //Permissions applied to remote files; null for default permissions. Can set user, Group, or other Read/Write/Execute permissions.
oTrRes.PreserveTimestamp = false; //Set last write time of destination file to that of source file - basically change the timestamp to match destination and source files.
oTrRes.ResumeSupport.State = TransferResumeSupportState.Off;
// Enumerate files to download
DateTime dtYesterday = DateTime.Today.AddDays(-1);
//IEnumerable<RemoteFileInfo> fileInfos2 = session.EnumerateRemoteFiles(sRemotePath, "APBS*.XML", EnumerationOptions.None).Where(file => file.LastWriteTime > dtYesterday); // Chokes!
IEnumerable<RemoteFileInfo> fileInfos = session.EnumerateRemoteFiles(sRemotePath, "APBS*.XML", WinSCP.EnumerationOptions.None);