private void SFTPDownload(SessionOptions so, string OPFolderName, List <string> IPDir, List <string> OPdir) { try { session = new WinSCP.Session(); session.Open(so); for (int i = 0; i < IPDir.Count; i++) { string localPath = $"{OPFolderName}{OPdir[i]}"; localPath = DeserialiseXMLFromStr(localPath); Directory.CreateDirectory(localPath); string remotePath = IPDir[i]; remotePath = DeserialiseXMLFromStr(remotePath); // Enumerate files and directories to download IEnumerable <RemoteFileInfo> fileInfos = session.EnumerateRemoteFiles(remotePath, null, EnumerationOptions.EnumerateDirectories | EnumerationOptions.AllDirectories); foreach (RemoteFileInfo fileInfo in fileInfos) { string localFilePath = session.TranslateRemotePathToLocal(fileInfo.FullName, remotePath, localPath); if (fileInfo.IsDirectory) { // Create local subdirectory, if it does not exist yet Directory.CreateDirectory(localFilePath); } else { WriteToLogBox($"Downloading file {fileInfo.FullName}..."); // Download file string remoteFilePath = session.EscapeFileMask(fileInfo.FullName); TransferOperationResult transferResult = session.GetFiles(remoteFilePath, localFilePath); if (!transferResult.IsSuccess) { WriteToLogBox($"Error downloading file {fileInfo.FullName}: {transferResult.Failures[0].Message}"); } } } } } catch (Exception e) { WriteToLogBox($"Error: {e}"); } finally { session.Close(); session.Dispose(); GC.Collect(); } }
public static bool CheckForFiles() { string remoteDirectory = "/" + Properties.Settings.Default.FTPDownloadDir + "/"; string localDirectory = Properties.Settings.Default.RootFolderExt; bool bReturn = false; try { // Setup session options SessionOptions sessionOptions = new SessionOptions { Protocol = Protocol.Sftp, HostName = Properties.Settings.Default.FTPAddress, UserName = Properties.Settings.Default.FTPUserName, Password = Properties.Settings.Default.FTPPassword, SshHostKeyFingerprint = Properties.Settings.Default.SshHostKeyFingerprint }; using (WinSCP.Session session = new WinSCP.Session()) { // Connect session.Open(sessionOptions); //List files TransferOptions transferOptions = new TransferOptions(); transferOptions.TransferMode = TransferMode.Binary; RemoteDirectoryInfo files = session.ListDirectory(remoteDirectory); foreach (RemoteFileInfo file in files.Files) { if (file.Name.ToLower().Contains(".zip")) { GlobalServices.SetText("frmMain", "lFTPStatus", "Checking " + file.Name); if (!FileInloadStats.CheckRecordExists(file.Name)) { long total = file.Length > 0 ? file.Length : 1; int i = 0; GlobalServices.SetText("frmMain", "lFTPStatus", "Checking " + file.Name); //GlobalServices.SetProgressBar("frmMain", "pbProgress", 0, Convert.ToInt32(total), 0); GlobalServices.SetText("frmMain", "lFTPStatus", "Downloading File: " + file.Name); Console.WriteLine("Starting Download of file - " + file.Name + " Size - " + file.Length.ToString() + " Time - " + DateTime.Now.ToString()); // Download files TransferOperationResult transferOperationResult = session.GetFiles(session.EscapeFileMask(remoteDirectory + file.Name), localDirectory); // Check and throw if there are any errors with the transfer operation. transferOperationResult.Check(); if (transferOperationResult.IsSuccess) { var newFile = new FileInloadStats() { FileName = file.Name }; FileInloadStats.InsertRecord(newFile); Console.WriteLine("Completed Download of file - " + file.Name + " Size - " + file.Length.ToString() + " Time - " + DateTime.Now.ToString()); } } } } bReturn = true; } } catch (Exception e) { bReturn = false; return(bReturn); } return(bReturn); }