FTP – File Download and Upload using C# .NET
Today in this article, we will see how to perform C# .NET FTP – File Download and Upload operations.
We will explore this option using the SSH.NET library for .NET/.NET Core.
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
Create any .NET Core application like a console application,
Add SSH.NET Nuget package as below,
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 SFTPClient class that 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("****", "user", "****"))
{
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.
File Download using FTP
Downloading the file from a remote file path is pretty simple once the connection is established successfully.
Use the below code to download the file via stream
using (var client = new SftpClient("****", "user", "****"))
{
client.Connect();
ListDirectory(client, ".");
foreach (var file in files)
{
Console.WriteLine(file.FullName);
using (Stream stream = File.Create($"{destinationPath}{"/"}{ file.Name}"))
{
client.DownloadFile(file.FullName, fileStream);
}
}
client.Disconnect();
}
In the above code,
destinationPath – Is the file path where the file will be downloaded via SFTP and FTP
File Upload using FTP
File Upload using FTP is can be done using the SFTPClient object and calling the UploadFile method as explained below.
using (var client = new SftpClient("****", "user", "****"))
{
client.Connect();
ListDirectory(client, ".");
foreach (var file in files)
{
Console.WriteLine(file.FullName);
using (var uplfileStream = System.IO.File.OpenRead(localDirectoryFilePath))
{
client.UploadFile(uplfileStream, destinationPath, true);
}
}
client.Disconnect();
}
In the above code,
destinationPath – Is the file path where the file will be uploaded via SFTP and FTP
localDirectoryFilePath – File path from where the file will be uploaded to the remote path
FTP how to get a list of all files in a Path?
Below is the complete implementation for the ListDirectory() custom method which returns all the files available from the specified root folder and subfolders.
That’s all! Happy coding!
Does this help you fix your issue?
Do you have any better solutions or suggestions? Please sound off your comments below.
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.
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.