예제 #1
0
        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();
            }
        }