File Operations (Upload/Download/Delete) On FTP Location In C#

Introduction

In this article, we will learn how to Upload, Download and Delete file on FTP location.

Recently I have created a nuget package that will help you to upload, download, get all files and delete files on FTP location with less coding effort.

Here I will explain how you can use this plugin to achieve your FTP file operation requirement easily.

Plugin Details

Nuget Package Click
Github Repository Click

I have also created github repository with source code. You can also check out that to do customization and use the plugin.

How to Use

Step 1

First of all, you need to install nuget package from nuget package console or nuget package manager.

File Operations (Upload/Download/Delete) on FTP Location in C#

Step 2

After successful installation of nuget package, now you need to create an object of FTPHelper class by passing required details like host, username, password. These details depend on your connection settings.

class Program {
    static void Main(string[] args) {
        FTPHelper fTPHelper = new FTPHelper("hostname", "username", "password");
    }
}

 After creating object you can perform below operation.

Upload File

In upload file method, you need to pass FileStream and destination folder path where the file will be saved.

FTPHelper fTPHelper = new FTPHelper("hostname", "username", "password");
using(FileStream sr = new FileStream(@ "D:\Example\test.txt", FileMode.Open, FileAccess.Read)) {
    fTPHelper.UploadFile(sr, "destination-folder-path");
}

Download File

In download file method, you need to pass file name with full path as first parameter and local folder path as second parameter where file will be saved.

FTPHelper fTPHelper = new FTPHelper("hostname", "username", "password");
fTPHelper.DownloadFile("ftp-folder-path/filename.extension", @"D:\Example");

Delete File

In delete file method, you need to pass file name with full path as parameter.

FTPHelper fTPHelper = new FTPHelper("hostname", "username", "password");
fTPHelper.DeleteFile("ftp-folder-path/filename.extension");

Get all files from folder

In this method, you need to pass full folder path from which you want to read all files.

FTPHelper fTPHelper = new FTPHelper("hostname", "username", "password");
var files = fTPHelper.GetFilesFromFolder("/ftp-folder-path");

This method will return all file's name as List array. And now if you want to download them, then you need to download them one by one by calling Download File method.

Summary

In this article, I have explained how to upload, download and delete file from FTP with less effort by using my nuget package. Hope this will help you. Let me know in comments if you have any query.

Thanks


Similar Articles