• Loading...
ComponentPro UltimateSftp

Download remote files

Language Filter: AllSend comments on this topic to ComponentPro

To download multiple remote files (e.g. all ".dat" files), use the Download methods. The most used Download method overload just needs two parameters. The first one is the path to the remote directory and the second one is the path to the local directory.

using ComponentPro;
using ComponentPro.IO;
using ComponentPro.Net;

...

// Create a new class instance.
Sftp client = new Sftp();

// Connect to the SFTP server.
client.Connect("myserver");

// Authenticate.
client.Authenticate("userName", "password");

// ... 
 
// Get all directories, subdirectories, and files from remote folder '/myfolder' to 'c:\myfolder'.
client.Download("/myfolder", "c:\\myfolder");

// Get all directories, subdirectories, and files that match the specified search pattern from remote folder '/myfolder2' to 'c:\myfolder2'.
client.Download("/myfolder2", "c:\\myfolder2", "*.cs");

// or you can simply put wildcard masks in the source path, our component will automatically parse it. 
// download all *.css files from remote folder '/myfolder2' to local folder 'c:\myfolder2'.
client.Download("/myfolder2/*.css", "c:\\myfolder2");

// Download *.cs and *.vb files from remote folder '/myfolder2' to local folder 'c:\myfolder2'.
client.Download("/myfolder2/*.cs;*.vb", "c:\\myfolder2");

// Get files in the folder '/myfolder2' only.
TransferOptions opt = new TransferOptions(true, false, OptionValue.Auto, null, FileOverwriteMode.Overwrite, SymlinksResolveAction.Skip);
client.Download("/myfolder2", "c:\\myfolder2", opt);

// ... 
 
// Disconnect.
client.Disconnect();
To understand more about wildcard masks and search criterion, see this topic.

Download specific files and directories

To download just some specific files and directories, use the Download method overloads that have remoteFilesToTransfer or sourceFilesToTransfer parameter like this Download overload.

using ComponentPro.IO;
using ComponentPro.Net;

...

// Create a new class instance.
Sftp client = new Sftp();

// Connect to the SFTP server.
client.Connect("localhost");

// Authenticate.
client.Authenticate("test", "test");

// ... 
 
// List of files and directories to download. 
string[] files = new string[] { "myfile", "my dir", @"/my folder/my dir2" };

// Download selected files and subdirectories in remote folder '/my folder' to the local dir 'c:\temp'.
client.Download("/my folder", false, files, @"c:\temp", new TransferOptions());            

// Download files in the list. All files will be downloaded to the destination folder.
client.Download(files, @"c:\temp2", new TransferOptions());

// You can also use a list of FileInfoBase instances. 
// Get a list of files with size in between 5 bytes and 1M.
FileInfoCollection col = client.ListDirectory("my folder", new SizeSearchCondition(5, 1024 * 1024));

// You can alter the list here. 
// Now we upload the files in the list to the server.
client.Download(col, "c:\\new folder2", new TransferOptions());

// ... 
 
// Disconnect.
client.Disconnect();