Reputation: 1
sFTP folder structure:
MainFolder
|_FolderA
|_sub1
|_file1.txt
|_sub1
|_file2.txt
.
.
.
|_sub-n
|_filen.txt
|_FolderB
|_sub1
|_file3.txt
|_sub1
|_file4.txt
.
.
.
|_sub-n
|_filen.txt
Using Tamir's dll, can the above folder structure be downloaded from sftp?
using Tamir.Sharpssh;
using Tamir.Streams;
try
{
.
.
.
string[] s = Directory.GetFiles(ftpfolder,"*.txt", SearchOption.AllDirectories);
for(int i=0; i< s.length; i++)
{
osftp.Get(ftpfolder + s[i], localfolder + Path.GetfileName(s.[i]));
}
}
catch(IOException copyError)
{
logg(copyerror.message);
}
logg() is a function for logging errors encountered.
Tried generating errorlogs but none were logged. Any ideas anyone?
Upvotes: 0
Views: 1599
Reputation: 2442
If you copy files and directories recursively like 'scp -r user@host:/dir/on/remote C:\dir\on\local\', you can do this as follows.
using Tamir.SharpSsh;
var host = "host address";
var user = @"user account";
var password = @"user password";
var scp = new Scp( host, user, password );
scp.Connect();
scp.Recursive = true;
var remotePath = @"/dir/on/remote";
var localPath = @"C:\dir\on\local\";
scp.Get( remotePath, localPath );
scp.Close();
Upvotes: 0
Reputation: 9725
My idea is to use WinSCP it's pretty well documented with some nice examples... SharpSSH is very old and I believe no longer maintained / out of date...
Here's an example of usage...
using System;
using WinSCP;
class Example
{
public static int Main()
{
SessionOptions sessionOptions = new SessionOptions
{
Protocol = Protocol.Sftp,
HostName = EdiConfiguration.FtpIpAddressOrHostName,
UserName = EdiConfiguration.FtpUserName,
Password = EdiConfiguration.FtpPassword,
SshHostKeyFingerprint = EdiConfiguration.SshHostKeyFingerprint,
PortNumber = EdiConfiguration.FtpPortNumber
};
using (Session session = new Session())
{
session.Open(sessionOptions);
TransferOptions transferOptions = new TransferOptions();
transferOptions.TransferMode = TransferMode.Binary;
transferOptions.ResumeSupport.State = TransferResumeSupportState.Off;
// Download the files in the OUT directory.
TransferOperationResult transferOperationResult = session.GetFiles(EdiConfiguration.FtpDirectory, EdiConfiguration.IncommingFilePath, false, transferOptions);
// Check and throw if there are any errors with the transfer operation.
transferOperationResult.Check();
// Remove files that have been downloaded.
foreach (TransferEventArgs transfer in transferOperationResult.Transfers)
{
RemovalOperationResult removalResult = session.RemoveFiles(session.EscapeFileMask(transfer.FileName));
if (!removalResult.IsSuccess)
{
eventLogUtility.WriteToEventLog("There was an error removing the file: " + transfer.FileName + " from " + sessionOptions.HostName + ".", EventLogEntryType.Error);
}
}
}
}
}
Upvotes: 1