Using ComponentSoft FTP & SFTP Libraries to Transfer Files Between FTP & SFTP Servers

To upload a file from other file system to an FTP file system, simply use the CopyFrom methods.

The following example demonstrates how to connect to FTP and SFTP servers, and use the CopyFrom method to directly copy a file from the SFTP file system to the FTP file system. If you need to upload files and directories within a ZIP file to the SFTP server, see the topic Uploading files and directories within a ZIP file to an FTP server.

Remember to add using directives to your code to create aliases for existing namespaces and avoid having to type the fully qualified type names. The namespaces are ComponentSoft.Net and ComponentSoft.IO.

Copying files from an SFTP server to an FTP server. There is no temporary files created on the local PC.

C#
// Connect to an FTP file system.
Ftp ftpsys = new Ftp();
ftpsys.Connect(
"192.168.126.128", 21);
ftpsys.Authenticate(
"test", "test");
// Connect to an SFTP file system.
Sftp sftp = new Sftp();
sftp.Connect(
"192.168.126.128", 2222);
sftp.Authenticate(
"test", "test");
// Copy 'blog.txt' file from the SFTP file system to the FTP file system.
ftpsys.CopyFrom(sftp, "blog.txt", "my blog on FTP file system.txt");
ftpsys.Disconnect();
sftp.Disconnect();

VB.NET
' Connect to an FTP file system.
Dim ftpsys As New Ftp()
ftpsys.Connect("192.168.126.128", 21)
ftpsys.Authenticate("test", "test")
' Connect to an SFTP file system.
Dim sftp As New Sftp()
sftp.Connect("192.168.126.128", 2222)
sftp.Authenticate("test", "test")
' Copy 'blog.txt' file from the SFTP file system to the FTP file system.
ftpsys.CopyFrom(sftp, "blog.txt", "my blog on FTP file system.txt")
ftpsys.Disconnect()
sftp.Disconnect()

 

Filed under  //

Comments [0]

Downloading files using multiple threads

Downloading files and directories from the remote SFTP server to the local disk is quite simple with ComponentSoft Sftp. All that needs to occur is passing appropriate server information such as host name, port, user name, password/private key and the number of threads to the DownloadFiles method of the Sftp class.

The following steps illustrate how to use the DownloadFiles to download files and directories.

Downloading files using multiple threads

  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:
    C#  
     using ComponentSoft.IO;
    VB.NET  
    Imports ComponentSoft.IO
  2. Create a new instance of the Sftp class.
    C#  
    // Create a new instance.
    Sftp client = new Sftp();
    VB.NET  
    ' Create a new instance.
    Dim client As New Sftp()
  3. Register event handlers to the ThreadCompleted and ThreadsCompleted events to get informed when a thread has completed and all threads have completed. The code looks similar to the following:
    C#  
    client.ThreadCompleted += client_ThreadCompleted;
    client.ThreadsCompleted += client_ThreadsCompleted;
    VB.NET  
    AddHandler client.ThreadCompleted, AddressOf client_ThreadCompleted
    AddHandler client.ThreadsCompleted, AddressOf client_ThreadsCompleted
  4. Now pass all needed parameters to the DownloadFiles method. The code looks similar to the following:
    C#  
    // Download files and subdirectories from "/my folder" to "c:\\my folder" using 3 threads. This waits untils these threads complete.
    client.DownloadFiles("/my folder", "c:\\my folder", 3, true);
    VB.NET  
    ' Download files and subdirectories from "/my folder" to "c:\\my folder" using 3 threads. This waits untils these threads complete.
    client.DownloadFiles("/my folder", "c:\my folder", 3, True)

Final example code

C#  
public void DoMultiThreadsDownloadFiles()
{
   
// Create a new instance.
   
Sftp client = new Sftp();
   
// Connect to the server.
   
client.Connect("server");
   
// Authenticate.
   
client.Authenticate("user", "pass");
   client.CommandResponse += client_ResponseRead;
   client.ThreadCompleted += client_ThreadCompleted;
   client.ThreadsCompleted += client_ThreadsCompleted;
   
// ...
   
// Download files and subdirectories from "/my folder" to "c:\\my folder" using 3 threads. This waits untils these threads complete.
   
client.DownloadFiles("/my folder", "c:\\my folder", 3, true);
   
// ...
   
client.Disconnect();
}
void client_ThreadsCompleted(object sender, ThreadsCompletedEventArgs e)
{
   Console.WriteLine(
"Multi-threads file transfer completed");
}
void client_ThreadCompleted(object sender, ThreadCompletedEventArgs e)
{
   Console.WriteLine(
string.Format("Thread ID {0} completed", e.FileSystem.ThreadId));
}
void client_ResponseRead(object sender, CommandResponseEventArgs e)
{
   Sftp client = (Sftp)sender;
   
if (client.ThreadId >= 0)
       
if (e.Command != null)
           Console.WriteLine(
"Thread: {0} - CMD>       {1}", client.ThreadId,
               e.Command);
       
else
           
Console.WriteLine("Thread: {0} - RESPONSE>  {1}", client.ThreadId,
               e.Response);
}
VB.NET  
Public Sub DoMultiThreadsDownloadFiles()
    ' Create a new instance.
    Dim client As New Sftp()
    ' Connect to the server.
    client.Connect("server")
    ' Authenticate.
    client.Authenticate("user", "pass")
    AddHandler client.CommandResponse, AddressOf client_ResponseRead
    AddHandler client.ThreadCompleted, AddressOf client_ThreadCompleted
    AddHandler client.ThreadsCompleted, AddressOf client_ThreadsCompleted
    ' ...
    ' Download files and subdirectories from "/my folder" to "c:\\my folder" using 3 threads. This waits untils these threads complete.
    client.DownloadFiles("/my folder", "c:\my folder", 3, True)
    ' ...
    client.Disconnect()
End Sub
Private Sub client_ThreadsCompleted(ByVal sender As Object, ByVal e As ThreadsCompletedEventArgs)
    Console.WriteLine("Multi-threads file transfer completed")
End Sub
Private Sub client_ThreadCompleted(ByVal sender As Object, ByVal e As ThreadCompletedEventArgs)
    Console.WriteLine(String.Format("Thread ID {0} completed", e.FileSystem.ThreadId))
End Sub
Private Sub client_ResponseRead(ByVal sender As Object, ByVal e As CommandResponseEventArgs)
    Dim client As Sftp = CType(sender, Sftp)
    If client.ThreadId >= 0 Then
        If e.Command IsNot Nothing Then
            Console.WriteLine("Thread: {0} - CMD> {1}", client.ThreadId, e.Command)
        Else
            Console.WriteLine("Thread: {0} - RESPONSE> {1}", client.ThreadId, e.Response)
        End If
    End If
End Sub

 

How to upload selected files and directories with UltimateSftp

Use the UploadFiles method to easily upload selected files and directories from the local disk to the SFTP server. You just need to provide local path, remote path, files and directories to upload and transfer options, ComponentSoft Ultimate SFTP component will do the rest of hard work for you. Other examples for FTP can also be found at FTP blog.

The following steps show you how to use the UploadFiles method to upload multiple files to the remote server.

C#



// Create a new 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.UploadFiles(@"c:\my folder", files, "/temp", new TransferOptions());
 // ...
 // Disconnect.
 client.Disconnect();


VB.NET

' Create a new instance.
 Dim client As New Sftp()
 ' Connect to the SFTP server.
 client.Connect("localhost")
 ' Authenticate.
 client.Authenticate("test", "test")
 ' ...
 ' List of files and directories to upload.
 Dim files() As 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.UploadFiles("c:\my folder", files, "/temp", New TransferOptions())
 ' ...
 ' Disconnect.
 client.Disconnect()

What's ComponentSoft.net Ultimate SFTP? What are its benefits?

ComponentSoft.net Ultimate SSH Secure File Transfer (SFTP) .NET Component offers a comprehensive interface for SFTP, enabling you to quickly and easily incorporate SSH Secure File Transfers in your applications, as well as remote file management using SFTP. In addition to downloading and uploading by file nameURL, and wild card masks, the SFTP library also supports remote file management functionality such as directory listingsand the ability to renamedelete and move files on the server. The component also offers the flexibility, ease of use and rapid development features of a component without the complexities of working with the native socket class or in-depth knowledge of how the File Transfer Protocols are implemented. In most cases, only a few lines of code are required to implement a file transfer in your application. The set of properties and methods is sufficiently rich to enable you to take advantage of features such as the resumption of interrupted transfers, passive mode operation in the presence of firewalls, automatic file verification and support forcustom server commands.

In addition to supporting standard file transfers, ComponentSoft.net Ultimate SFTP Component also supports multi-thread file transfers as well as data compression on-the-fly with built-in Zlib classes to speed up the transfers.

To reduce your effort writing a number of classes for different file systems such as FTP, SFTP, ZIP, Local Disk, and Virtual Disk, we introduce the Unified File System that makes the management of files on these systems seamless. Moreover, it allows you to use the same code to transfer files and directories directly from one to other file systems. As a benefit, you should only need to write one class that works with these file systems nicely. There is no need to learn more about other File Transfer Protocols, all the complicated work is done by the Ultimate File System. For more information, please visit this topic.

To help you to become familiar with the features of the component, a number of well documented samples and topics were added to the comprehensive integrated online documentation which can be found at this web page.

The following is a list of some key features of the component:

  • Upload, download, append, rename and delete SFTP file
  • Large file support (can be greater than 4GB)
  • Parse listings automatically
  • Supports both ASCII and binary transfers
  • Supports restarting interrupted uploads and downloads
  • Abort operations smoothly
  • Synchronizes folders easily
  • Upload, download, or Remove entire directory (including subdirectories and files) quickly with a single line of code
  • Supports Multi-threading. You can use as many threads as you want to speed up transferring files
  • File transfer monitoring support with progress event
  • Fully supports both event-driven (asynchronous) and blocking (synchronous) application designs
  • Send and receive files to or from disk or memory streams. This allows you to compress and decompress ZIP files on-the-fly
  • Unified Remote File System Architecture which allows you to directly and easily transfer and synchronize files between an SFTP file system and other file systems such as FTP, SCP, ZIP, Disk, Memory, etc.
  • Built-in Zlib streams
  • Filter files on name with wildcards mask or regular expression, size, or last modified date
  • Upload and download multiple files using wildcards mask with a single line of code
  • Resume previously interrupted file
  • Allows you to check the exact transferring state like uploading, downloading, etc.
  • Progress event argument contains full information about the file transfer such as: File size, percentage, total percentage, start time, transfer speed, time left, etc.
  • SFTP protocol version 3 support
  • Thoroughly tested with Bitvise WinSSHD, CoreFTP Server, GlobalScape Security Server, OpenSSH, WeOnlyDo wodSFTP and much more
  • Compliant with RFC 4250-4254, 4256 and 4419
  • Supports GSSAPI authentication (gssapi-with-mic) added. Kerberos and NTLM mechanisms are supported
  • FIPS 140-2 compliant. This mode is automatically enabled on systems where only compliant algorithms are allowed
  • Encryption/decryption using Triple DES, RC4 or AES
  • RSA and DSA public keys, public key authentication
  • Diffie Hellman key exchange

Security and Reliability

ComponentSoft Ultimate SFTP component provides industry standard security using the SSH Secure File Transfer Protocol. Our components support strong, commercial grade encryption, and we do not rely on any third-party toolkits which have licensing restrictions or are encumbered by patents. By setting a single property or option, the component automatically handles all of the complex certificate management, protocol negotiation and encryption for you. Even advanced options, such as using client certificates, are handled easily with just a few lines of code. 

Supports HTTP Connect, SOCKS4, SOCKS4A & SOCKS5 Proxy Servers

Ultimate SFTP provides full support for Proxy Servers such as HTTP CONNECT, SOCKS4, SOCKS4A, and SOCKS5. By simply setting a few properties, you are able to download and upload files and directories from/to SFTP servers through the desired proxy server, providing your application with the greatest flexibility and highest level of security available. 

Support for IPv6, the next generation Internet protocol

IPv6 (Internet Protocol version 6), the next-generation protocol designed by the IETF (Internet Engineering Task Force), will gradually replace the current version of the Internet Protocol, IPv4.

With Ultimate SFTP Component, users have an integrated service assurance solution that will provide them with SFTP, and Proxy capabilities in mixed IPv4 and IPv6 environments. This helps organizations with the adoption of and the transition to IPv6 by making the management of such networks seamless. 

.NET Technology

By using 100% managed code written in C#, the Ultimate SFTP component takes advantage of the numerous built-in features of the .NET Framework to enhance performance, moreover, the component is CLS compliant, and it does not use any unsafe blocks for minimal permission requirements. 

ComponentSoft SFTP Supports many .NET Platforms

Ultimate SFTP Component for .NET supports the following platforms:

  • Windows Forms
  • Web Forms
  • Web Services

In addition, it is also possible to use the component in PowerShell - Microsoft's new command console and scripting language.

Ultimate SFTP is fully compatible with Visual Studio 2005 and Visual Studio 2008, as well as upcomming release of Visual Studio 2010. As a benefit, you are always up-to-date with Microsoft's Technologies when using our products. 

Supports event-driven (Asynchronous) and blocking (Synchronous) Design

Most applications written with the SFTP component will be synchronous. Synchronous method gives you the ease-of-use, but it can only returns the control back to the caller after it has finished, meaning that it blocks the execution of the caller for a period of time. Using synchronous method is recommended when you only need to execute one SFTP operation at a time.

You might decide that your design requires an asynchronous operation when it is needed to manage email messages simultaneously. Asynchronous methods provide a great deal of power. Asynchronous method is executed on a thread separate from the calling thread. Such operation is useful when an SFTP operation is time consuming and other codes need to execute without waiting for the initial operation to complete. In addition, the user interface will be most responsive when asynchronous methods are used. 

Flexibility

The SFTP component has been designed with a great degree of flexibility and can be used with a wide variety of programming languages and different types of development environments. Available as a managed .NET class, it is fully supported by languages such as Visual Basic, Visual C#, J#, Managed C++, Borland C# Builder, and Delphi. 

Fully Documented

As important as functionality, features and stability are, comprehensive documentation is equally as important to the application developer. This is why the ComponentSoft SFTP for .NET product includes a Developer's Guide and a complete Technical Reference which documents every property, method and event supported by the component. A printable version of the documentation is included with the product, as well as context-sensitive online help which can be accessed directly from within the development environment. 

Lots of complete SFTP client samples in VB.NET, C#, and ASP.NET

In addition to the fully documented Developer's Guide and a complete Technical Reference, Ultimate SFTP component also includes a number of samples with full source code which help you to become familiar with the features of the component and provide code which you can re-use in your own applications. The setup package contains three complete SFTP client applications: SFTP Console Client, SFTP WinForms Client, and SFTP AJAX WebForms Client. 

Royalty-free

ComponentSoft understands today's software development requirements which often require support for multiple operating systems and different programming languages. As with all Ultimate components, the product is licensed to a single developer, and applications built using the SFTP component can be redistributed to as many end-users as needed without additional royalties or runtime licensing fees. Developers are also permitted to install the product on different development systems as long as they are the only one using it and there is no chance that it can be used by more than one developer at the same time. 

 

For more information, please checkout the following links:

Product page link: http://www.componentsoft.net/component/sftp.net/

Filed under  //

Comments [0]