Home > Articles > .Net Framework Articles > How to upload multiple files and directories to an SFTP server

How to upload multiple files and directories to an SFTP server

by Jimmy Osmond   on Nov 26, 2014   Category: .Net Framework  | Level: Beginner  |  Views: 2920    |  Points: 100   
Like this article? Bookmark and Share:
This shows how to upload multiple files to an SFTP server using Ultimate SFTP library.

You often use an SFTP Client like FileZila to manually transfer files and folders to an SFTP or FTP server. However that tool does allow you to automate your transfer. That is when you need to write a program to do that for you. One option is to write your own code to do such things. However that is a tremendous work that require lots of effort to understand SSH File Transfer protocol. Another solution is to use a third-party component. Ultimate SFTP component for .NET is a 100%-managed .NET class library that allows you to easily add SSH Secure File Transfer (SFTP) capabilities to your .NET and ASP.NET applications.

This article show you how to use the UploadFiles method to upload multiple files and folders to an SFTP server.

  1. Add using directives to your code to create aliases for existing namespaces and avoid having to type the fully qualified type names. The code looks similar to the following:
    using ComponentPro.Net;
  2. Create a new instance of the Sftp class.
  3. Now you can connect to the SFTP server with Connect methods. The code looks similar to the following:
    // Create a new instance.
    Sftp client = new Sftp();
    
    // Connect to the SFTP server.
    client.Connect("localhost");
    
  4. Use your user name and password to login with Authenticate methods, or use your user name and a private key to login. The code looks similar to the following:
    // Authenticate.
    client.Authenticate("test", "test");
  5. If you wish to control the file upload process, you might need to handle the TransferConfirm and Progress events. Check out SftpClient, SftpMultipleThreadsTransfer and SftpTransferMultipleFiles sample projects for more details on how to handle these events.
  6. Now pass the path of the local directory and the path of the remote directory to the UploadFiles method. The code looks similar to the following:
    // Upload all files and subdirectories from local folder 'c:\temp' to the remote dir '/temp'
    client.UploadFiles("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.UploadFiles("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.UploadFiles("c:\\myfolder2\\*.css", "/myfolder2");
    
    // Upload *.cs and *.vb files from local folder 'c:\myfolder2' to remote folder '/myfolder2'.
    client.UploadFiles("c:\\myfolder2\\*.cs;*.vb", "/myfolder2");
  7. After completing your work, call the Disconnect method to close the SFTP session.

Complete Example:

using ComponentPro.Net;

...

// Create a new 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.UploadFiles("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.UploadFiles("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.UploadFiles("c:\\myfolder2\\*.css", "/myfolder2");

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

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


Like this article? Bookmark and Share:

Most viewed Articles

User Comments


No response found, be the first to review this article.

Submit feedback about this article

Please sign in to post feedback

Latest Posts