Example #1
0
        static void DownloadFiles()
        {
            SessionOptions sessionOptions = new SessionOptions
            {
                Protocol = Protocol.Ftp,
                HostName = ApplicationSetting.FtpHost,
                UserName = ApplicationSetting.FtpUserName,
                Password = ApplicationSetting.FtpPassword //,
                                                          //SshHostKeyFingerprint = "ssh-rsa 2048 xx:xx:xx:xx:xx:xx:xx:xx..."
            };

            string remotePath = ApplicationSetting.FtpRemoteFolder;
            string localPath  = ApplicationSetting.LatestFilesPath;

            using (Session session = new Session())
            {
                // Connect
                session.Open(sessionOptions);

                // Enumerate files and directories to download
                IEnumerable <RemoteFileInfo> fileInfos =
                    session.EnumerateRemoteFiles(
                        remotePath, null,
                        EnumerationOptions.EnumerateDirectories |
                        EnumerationOptions.AllDirectories);

                foreach (RemoteFileInfo fileInfo in fileInfos)
                {
                    string localFilePath =
                        RemotePath.TranslateRemotePathToLocal(
                            fileInfo.FullName, remotePath, localPath);

                    if (fileInfo.IsDirectory)
                    {
                        // Create local subdirectory, if it does not exist yet
                        if (!Directory.Exists(localFilePath))
                        {
                            Directory.CreateDirectory(localFilePath);
                        }
                    }
                    else
                    {
                        Console.WriteLine("Downloading file {0}...", fileInfo.FullName);
                        // Download file
                        string remoteFilePath = RemotePath.EscapeFileMask(fileInfo.FullName);
                        TransferOperationResult transferResult =
                            session.GetFiles(remoteFilePath, localFilePath);

                        // Did the download succeeded?
                        if (!transferResult.IsSuccess)
                        {
                            // Print error (but continue with other files)
                            Console.WriteLine(
                                "Error downloading file {0}: {1}",
                                fileInfo.FullName, transferResult.Failures[0].Message);
                        }
                    }
                }
            }
        }
Example #2
0
        static internal void WinScpGetLog()
        {
            LogEvent("Start Download");
            using (Session session = new Session())
            {
                // Connect
                session.Open(GetWinScpSession());

                string remotePath = ApplicationSetting.FtpRemoteFolder;
                string localPath  = ApplicationSetting.DownloadPath;

                // Get list of files in the directory
                RemoteDirectoryInfo directoryInfo = session.ListDirectory(remotePath);

                //latest file name in the table by application name
                var latestLogFileInDb = Path.GetFileName(unitOfWork.IISLogRepository.LatestFileName(ApplicationSetting.ApplicationName));

                //get the date of the latest file from FTP
                var logFileDate = directoryInfo.Files
                                  .Where(w => w.Name.ToLower() == latestLogFileInDb?.ToLower())
                                  .Select(s => s.LastWriteTime).FirstOrDefault();

                // Select the files not in database table
                IEnumerable <RemoteFileInfo> notInLogTable =
                    directoryInfo.Files
                    .Where(file => !file.IsDirectory && file.LastWriteTime > logFileDate).ToList();

                //// Download the selected file
                foreach (RemoteFileInfo fileInfo in notInLogTable)
                {
                    string localFilePath =
                        RemotePath.TranslateRemotePathToLocal(
                            fileInfo.FullName, remotePath, localPath);

                    string remoteFilePath = RemotePath.EscapeFileMask(fileInfo.FullName);

                    //download
                    TransferOperationResult transferResult =
                        session.GetFiles(remoteFilePath, localFilePath);
                }
                LogEvent("End Download");
            }
        }
Example #3
0
        /// <summary>
        /// </summary>
        /// <param name="session"></param>
        /// <param name="remotePath"></param>
        /// <param name="localPath"></param>
        public void DowloadFilesAndDirectories(SessionOptions options, string remotePath, string localPath)
        {
            using var session = new Session();
            // Connect
            session.Open(options);

            var fileInfos = GetFilesAndDirectoriesRemoteDir(options, remotePath);

            foreach (var fileInfo in fileInfos)
            {
                var localFilePath =
                    RemotePath.TranslateRemotePathToLocal(
                        fileInfo.FullName, remotePath, localPath);

                if (fileInfo.IsDirectory)
                {
                    // Create local subdirectory, if it does not exist yet
                    if (!Directory.Exists(localFilePath))
                    {
                        Directory.CreateDirectory(localFilePath);
                    }
                }
                else
                {
                    Console.WriteLine("Downloading file {0}...", fileInfo.FullName);
                    // Download file
                    var remoteFilePath = RemotePath.EscapeFileMask(fileInfo.FullName);

                    var transferResult =
                        session.GetFiles(remoteFilePath, localFilePath);

                    // Did the download succeeded?
                    if (!transferResult.IsSuccess)
                    {
                        // Print error (but continue with other files)
                        Console.WriteLine(
                            "Error downloading file {0}: {1}",
                            fileInfo.FullName, transferResult.Failures[0].Message);
                    }
                }
            }
        }