• Loading...
ComponentPro UltimateSftp

Upload files

Language Filter: AllSend comments on this topic to ComponentPro

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

using System;
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");

// ... 
 
// Upload all files and subdirectories from local folder 'c:\temp' to the remote dir '/temp'
client.Upload("c:\\temp", "/temp");

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

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

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

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

Upload specific files and directories

To upload just some specific files and directories, use the Upload method overloads that have localFilesToTransfer or sourceFilesToTransfer parameter like this Upload 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 upload. 
string[] files = new string[] { "myfile", "my dir", @"c:\my folder\my dir2" };

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

// Upload files in the list. All files will be uploaded to the destination folder.
client.Upload(files, "/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 = DiskFileSystem.Default.ListDirectory(@"c:\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.Upload(col, "/new folder2", new TransferOptions());

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