/// <summary>
/// Downloads the files.
/// </summary>
/// <param name="fileList">The file list.</param>
/// <param name="failRemoteNotExists">if set to <c>true</c> [fail remote not exists].</param>
/// <exception cref="System.Exception">
/// Local File Already Exists.
/// </exception>
public void DownloadFiles(List<ISFTPFileInfo> fileList)
{
try
{
this.Log(String.Format("Connecting to Host: [{0}].", this.hostName), LogLevel.Minimal);
using (SftpClient sftp = new SftpClient(this.hostName, this.portNumber, this.userName, this.passWord))
{
sftp.Connect();
this.Log(String.Format("Connected to Host: [{0}].", this.hostName), LogLevel.Verbose);
// Download each file
foreach (SFTPFileInfo filePath in fileList)
{
if (!sftp.Exists(filePath.RemotePath))
{
this.Log(String.Format("Remote Path Does Not Exist: [{0}].", this.hostName), LogLevel.Verbose);
if (this.stopOnFailure)
throw new Exception(String.Format("Remote Path Does Not Exist: [{0}]", filePath.RemotePath));
else
continue;
}
if (Directory.Exists(filePath.LocalPath))
filePath.LocalPath = Path.Combine(filePath.LocalPath, filePath.RemotePath.Substring(filePath.RemotePath.LastIndexOf("/") + 1));
// Can we overwrite the local file
if (!filePath.OverwriteDestination && File.Exists(filePath.LocalPath))
throw new Exception("Local File Already Exists.");
this.Log(String.Format("Downloading File: [{0}] -> [{1}].", filePath.RemotePath, filePath.LocalPath), LogLevel.Minimal);
using (FileStream fileStream = File.OpenWrite(filePath.LocalPath))
{
sftp.DownloadFile(filePath.RemotePath, fileStream);
}
this.Log(String.Format("File Downloaded: [{0}]", filePath.LocalPath), LogLevel.Verbose);
// Remove remote file
if (filePath.RemoveSource)
{
this.Log(String.Format("Removing File: [{0}]", filePath.RemotePath), LogLevel.Minimal);
sftp.Delete(filePath.RemotePath);
this.Log(String.Format("Removed File: [{0}]", filePath.RemotePath), LogLevel.Verbose);
}
}
}
this.Log(String.Format("Disconnected from Host: [{0}].", this.hostName), LogLevel.Minimal);
}
catch (Exception ex)
{
this.Log(String.Format("Disconnected from Host: [{0}].", this.hostName), LogLevel.Minimal);
this.ThrowException("Unable to Download: ", ex);
}
}