Retrieve a file from a server via SFTP- .NET C#

Retrieve a file from a server via SFTP

Today in this article, we will see how to Retrieve a file from a server via SFTP or FTP – .NET C#.

We will use a .NET library called SSH.NET to perform SFTP Get a list of Files recursively. SSH.NET library works fine in regular .NET and .NET core seamlessly.

SSH.NET is a Secure Shell (SSH-2) library for .NET which provides the capability to execute various SSH commands and provides SFTP ( Secured File transfer) features like Upload or download.

What is SFTP

SFTP (SSH File Transfer Protocol) is a secure file transfer protocol.

It runs over the SSH protocol. It supports the full security and authentication functionality of SSH.

Below is the list of high-level features supported by the SSH library (but not limited to),

  • Provide SFTP functionality for both synchronous and asynchronous operations.

  • Provides SCP functionality

  • Execution of SSH command using both synchronous and asynchronous methods

  • Provide a status report for upload and download SFTP operations to allow accurate progress bar implementation.

  • Supports public-key, password, and keyboard-interactive authentication methods.

  • Supports two-factor or higher authentication

  • Supports SOCKS4, SOCKS5, and HTTP Proxy

Getting access to all the files using the SSH.NET library is pretty simple.

Getting Started – Install Nuget SSH.NET

Create any .NET Core application like a console application,

Add SSH.NET Nuget package as below,

Retrieve a file from a server via SFTP

Package Manager Console,

Install-Package SSH.NET -Version 2020.0.2

Note: Please use the latest version

Connect to SFTP using C# .NET- SFTPClient

SSH.NET provides a simple class SFTPClient which lets you connect to the SFTP server securely and access files for

read, write, upload, or download purposes,

 

using (var client = new SftpClient("SFTP server IP", "username", "password"))
            {
                client.Connect();
                Console.WriteLine("Connected to host");
                client.ChangeDirectory(remoteBaseDirectory);

                Console.WriteLine("Changed directory to {0}", remoteBaseDirectory);
                Console.WriteLine("Listing directory:");

                ListDirectory(client, ".");
                foreach (var file in files)
                {
                  Console.WriteLine(file.FullName);
                }
             }




Above ChangeDirectory() method setup the remoteBaseDirectory specified path as the base path for the recursive function to get all files from the main and subfolder.

If not used, the default location will be used as the base path.

SFTP – Get a list of all files

Below is the complete implementation for the ListDirectory() custom method which returns all the files available from the specified root folder and subfolders.

SSHNET SFTP Get a list of files or directories recursively

The above method once executed, return all the files from the provided root folder.

SFTP Read file and download it

Let’s now read the FTP file iteratively.

Below we are reading each file from the FTP location and downloading it to the local or destination folder.


       foreach (var file in files)
                {
                    Console.WriteLine(file.FullName);
                    using (Stream fileStream = File.Create($"{destinationPath}{"/"}{ 
                    file.Name}"))
                    {
                        client.DownloadFile(file.FullName, fileStream);
                        
                    }
              }  


Where “destinationPath” is your custom path where you want to download/store the file

References :

Do you have any comments or ideas or any better suggestions to share?

Please sound off your comments below.

Happy Coding !!



Please bookmark this page and share it with your friends. Please Subscribe to the blog to receive notifications on freshly published(2024) best practices and guidelines for software design and development.



Leave a Reply

Your email address will not be published. Required fields are marked *