SSH.NET SFTP Get a list of directories and files recursively

Today in this article, we will see how to get a list of all files using SSH.NET. We will use a library called SSH.NET to perform SFTP Get a list of Files recursively.

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,

SSHNET SFTP Get a list of files recursively

Package Manager Console,

Install-Package SSH.NET -Version 2020.0.2

Note: Please use the latest version

Connect to SFTP using SFTPClient

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

read , write, upload or download purposes,

Below the sample code read the FTP file and write their name on the console.

 

using (var client = new SftpClient("****", "root", "****"))
            {
                client.Connect();
                ListDirectory(client, ".");
                foreach (var file in files)
                {
                  Console.WriteLine(file.FullName);
                }
             }




The above ListDirectory uses the default path to fetch a file from the SFTP server.

How to change the default file path in SFTP

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.

Example

client.ChangeDirectory(remoteBaseDirectory);

SFTP Read file recursively

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.

blank

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 *