Beispiel #1
0
        /// <summary>
        /// Function Name : SftpFileDownload()
        /// Description   : This method is used to download a file from SFTP Server to local machine.
        /// </summary>
        /// <param name="localPath">local file path </param>
        /// <param name="remoteFile">SFTP file name that need to be downloaded.</param>
        public bool SftpFileDownload(String localPath, String remoteFile)
        {
            var  fullFilepath = string.Empty;
            bool IsComplete   = true;

            Thread.Sleep(2000);
            try
            {
                if (!this.SftpConnect())
                {
                    this.SftpConnect();

                    if (!this.SftpConnect())
                    {
                        throw new Exception("Sftp not connected.");
                    }
                }


                //fullFilepath = objAdpterSettings.SourceLocation.TrimEnd('\\', '/') + "/" + remoteFile;

                if (File.Exists(localPath + "\\" + Path.GetFileName(remoteFile)))
                {
                    Random random = new Random();
                    File.Move(localPath + "\\" + Path.GetFileName(remoteFile), localPath + "\\" + random.Next() + "_" + Path.GetFileName(remoteFile));
                }
                _objSftpClient.GetFile(localPath, remoteFile);
            }
            catch (Exception ex)
            {
                Console.WriteLine("Error while downloading files from SFTP. Error" + ex.Message);
                IsComplete = false;
                throw ex;
            }
            return(IsComplete);
        }