Using IRemoteFileSystem to work with both SFTP and FTP without writing duplicate code.
The interface IRemoteFileSystem is designed to help developers write single fragment of code that can work with both SFTP and FTP. It has some common method for connecting, authenticating and disconnecting. The following example demonstrates how to use the interface to start a connection, authenticate, upload, download, and end the connection.
void SFTPandFTPConnection(IRemoteFileSystem client, string host, int port, string username, string password)
{
client.Connect(host, port);
client.Authenticate(username, password);
FileSystem system = client as FileSystem;
// Delete files
client.DeleteFiles("*.tmp");
// Create a new directory
client.CreateDirectory("my dir");
// Download files from the root directory.
TransferOptions opt = new TransferOptions();
opt.Recursive = true;
FileSystem.TransferFiles(system, "/", (IFileInfo[])null, DiskFileSystem.Default, @"C:\temp", opt);
// Upload files from C:\test to the root directory
FileSystem.TransferFiles(DiskFileSystem.Default, @"C:\Test", (IFileInfo[])null, system, "/", opt);
client.Disconnect();
}