Download, Upload, Delete Files From FTP Server Using C# PDF

Download as pdf or txt
Download as pdf or txt
You are on page 1of 9

Download, Upload,Delete Files from FTP Server Using C# | Cybarlab Página 1 de 9

Cybarlab
Home Programming » Framework » Database » Scripting » HTML/CSS »

Services » Reporting » Interview All Post

09 Mar 2013
Search this site...

Home » ASP.NET • C# » Download, Upload,Delete Files from Search


FTP Server Using C#

Popular Posts Recent Posts


Download, Upload,Delete CRUD operations in
AngularJS and Web API
Files from FTP Server Using
SQL Queries Optimization
C# Tips

Posted in ASP.NET, C# By Rashed On March 9, 2013 Find a string inside an


entire database
FTP is a file transfer protocol. We can use it in different ways. Lot
of third party software or tools (WinSCP, FireFTP, FileZilla etc)
are available for that. Some time we need to perform basic FTP
Find us
operation in C#. This article describes step by step Download,
Upload, Delete ftp in C# . Summary of the article:

◦ What is FTP?
◦ Show File List of the FTP Server in C#
◦ Download File from the FTP Server in C#
◦ Upload File from Local Machine to the FTP Server in C#
◦ Delete File from the FTP Server in C#
Esta página foi classificada como "Not Categorized" e o se
◦ Move File from One Directory to Another in the FTP Server in
C#
Para
◦ Check File Existence in the FTP ter ciência
Server in C#de quais as categorias de páginas (ou sites) bloqueadas, consulte o
Se você considera a categorização errada, por favor abra um chamado no

What is FTP?
File Transfer Protocol or FTP is a standard Internet protocol for
transferring files between computers on the network or internet. It

http://cybarlab.com/download-upload-files-from-ftp-server-in-c-sharp 2016/07/18
Download, Upload,Delete Files from FTP Server Using C# | Cybarlab Página 2 de 9

is an application protocol. Like others protocol (HTTP, SMTP)


FTP uses Internets TCP/IP protocols. FTP is most commonly
used to download a file from a server or to upload a file to a
server by using network or internet.
We can use FTP with a graphical FTP clients or command line
interface or web browser. Different programming language
support FTP. Using them we can develop programs to perform
some SFT Operations like delete, rename, move, and copy.

Show File List of FTP Server in C#


The following C# code will return the list of all the file names of a
FTP Server in a List. For this we need to include the following
namespace in the class:

using System.Net;

string _ftpURL = "testftp.com"; //Host URL or address of the FTP server


string _UserName = "admin"; //User Name of the FTP server
string _Password = "admin123"; //Password of the FTP server
string _ftpDirectory = "Receipts"; //The directory in FTP server where the files a
List F

public List ShowFileList(string ftpURL, string UserName, string Password, string ft


{
FtpWebRequest request = (FtpWebRequest)WebRequest.Create(ftpURL + "/" + ftpDire
request.Credentials = new NetworkCredential(UserName, Password);
request.Method = WebRequestMethods.Ftp.ListDirectory;
StreamReader streamReader = new StreamReader(request.GetResponse().GetResponseS
List lines = new List();
string line;

while ((line = streamReader.ReadLine()) != null)


{
lines.Add(line);
}
streamReader.Close();
return lines;
}

 

Download File from the FTP Server in C#


The following C# code will download all the files from the FTP
server into local machine.

 

http://cybarlab.com/download-upload-files-from-ftp-server-in-c-sharp 2016/07/18
Download, Upload,Delete Files from FTP Server Using C# | Cybarlab Página 3 de 9

string _ftpURL = "testftp.com"; //Host URL or address of the FTP server


string _UserName = "admin"; //User Name of the FTP server
string _Password = "admin123"; //Password of the FTP server
string _ftpDirectory = "Receipts"; //The directory in FTP server where the
string _FileName = "test1.csv"; //File name, which one will be download
string _LocalDirectory = "D:\\FilePuller"; //Local directory where the files will
DownloadFile(_ftpURL, _UserName, _Password, _ftpDirectory, _FileName, _LocalDirecto

public void DownloadFile(string ftpURL, string UserName, string Password, string ft


{
if (!File.Exists(LocalDirectory + "/" + FileName))
{
try
{
FtpWebRequest requestFileDownload = (FtpWebRequest)WebRequest.Create(ft
requestFileDownload.Credentials = new NetworkCredential(UserName, Passw
requestFileDownload.Method = WebRequestMethods.Ftp.DownloadFile;
FtpWebResponse responseFileDownload = (FtpWebResponse)requestFileDownlo
Stream responseStream = responseFileDownload.GetResponseStream();
FileStream writeStream = new FileStream(LocalDirectory + "/" + FileName
int Length = 2048;
Byte[] buffer = new Byte[Length];
int bytesRead = responseStream.Read(buffer, 0, Length);
while (bytesRead > 0)
{
writeStream.Write(buffer, 0, bytesRead);
bytesRead = responseStream.Read(buffer, 0, Length);
}
responseStream.Close();
writeStream.Close();
requestFileDownload = null;
}
catch (Exception ex)
{
throw ex;
}
}
}
 

Upload File from Local Machine to the FTP Server in C#


The following C# code will upload a file from local machine to FTP
server.

string _ftpURL = "testftp.com"; //Host URL or address of the FTP server



string _UserName = "admin"; //User Name of the FTP server
string _Password = "admin123"; //Password of the FTP server
string _ftpDirectory = "Receipts"; //The directory in FTP server where the
string _FileName = "test1.csv";  name, which one will be uploaded
//File
string
 _LocalDirectory = "D:\\FilePuller";//Local directory from where the files

http://cybarlab.com/download-upload-files-from-ftp-server-in-c-sharp 2016/07/18
Download, Upload,Delete Files from FTP Server Using C# | Cybarlab Página 4 de 9

UploadFile(_ftpURL, _UserName, _Password, _ftpDirectory, _FileName, _LocalDirectory

public void UploadFile(string ftpURL, string UserName, string Password, string ftpD
{
//Comming soon....
}
 

Delete File from the FTP Server in C#


The following C# code will delete a file from the FTP server.

string _ftpURL = "testftp.com"; //Host URL or address of the FTP server


string _UserName = "admin"; //User Name of the FTP server
string _Password = "admin123"; //Password of the FTP server
string _ftpDirectory = "Receipts"; //The directory in FTP server where the files w
string _FileName = "test1.csv"; //File name, which one will be uploaded
DeleteFile(_ftpURL, _UserName, _Password, _ftpDirectory, _FileName);

public void DeleteFile(string ftpURL, string UserName, string Password, string ftpD
{
FtpWebRequest ftpRequest = (FtpWebRequest)WebRequest.Create(ftpURL + "/" + ftpD
ftpRequest.Credentials = new NetworkCredential(UserName, Password);
ftpRequest.Method = WebRequestMethods.Ftp.DeleteFile;
FtpWebResponse responseFileDelete = (FtpWebResponse)ftpRequest.GetResponse();
}
 

Move File from One Directory to Another in the FTP Server in


C#
The following C# code will move file from one directory to another
in FTP server.

string _ftpURL = "testftp.com"; //HostURL or address of the FTP server


string _UserName = "admin"; //User Name of the FTP server
string _Password = "admin123"; //Password of the FTP server
string _ftpDirectory = "Receipts"; //The directory in FTP server where the fil
string _FileName = "test1.csv"; //File name, which one will be uploaded
string _ftpDirectoryProcessed = "Done"; //The directory in FTP server where the fil
MoveFile(_ftpURL, _UserName, _Password, _ftpDirectory, _FileName, _ftpDirectoryProc

public void MoveFile(string ftpURL, string UserName, string Password, string ftpDir
{
FtpWebRequest ftpRequest = null;
FtpWebResponse ftpResponse = null; 
try
 

http://cybarlab.com/download-upload-files-from-ftp-server-in-c-sharp 2016/07/18
Download, Upload,Delete Files from FTP Server Using C# | Cybarlab Página 5 de 9

{
ftpRequest = (FtpWebRequest)WebRequest.Create(ftpURL + "/" + ftpDirectory +
ftpRequest.Credentials = new NetworkCredential(UserName, Password);
ftpRequest.UseBinary = true;
ftpRequest.UsePassive = true;
ftpRequest.KeepAlive = true;
ftpRequest.Method = WebRequestMethods.Ftp.Rename;
ftpRequest.RenameTo = ftpDirectoryProcessed + "/" + FileName;
ftpResponse = (FtpWebResponse)ftpRequest.GetResponse();
ftpResponse.Close();
ftpRequest = null;
}
catch (Exception ex)
{
throw ex;
}
}
 

Check File Existence in the FTP Server in C#


The following C# code will check if a file is present or not in the
FTP server.

string _ftpURL = "testftp.com"; //Host URLor address of the FTP server


string _UserName = "admin"; //User Name of the FTP server
string _Password = "admin123"; //Password of the FTP server
string _ftpDirectory = "Receipts"; //The directory in FTP server where the file wi
string _FileName = "test1.csv"; //File name, which one will be checked
CheckFileExistOrNot(_ftpURL, _UserName, _Password, _ftpDirectory, _FileName);

public bool CheckFileExistOrNot(string ftpURL, string UserName, string Password, st


{
FtpWebRequest ftpRequest = null;
FtpWebResponse ftpResponse = null;
bool IsExists = true;
try
{
ftpRequest = (FtpWebRequest)WebRequest.Create(ftpURL + "/" + ftpDirectory +
ftpRequest.Credentials = new NetworkCredential(UserName, Password);
ftpRequest.Method = WebRequestMethods.Ftp.GetFileSize;
ftpResponse = (FtpWebResponse)ftpRequest.GetResponse();
ftpResponse.Close();
ftpRequest = null;
}
catch (Exception ex)
{
IsExists = false;
}
return IsExists; 
}
 

http://cybarlab.com/download-upload-files-from-ftp-server-in-c-sharp 2016/07/18
Download, Upload,Delete Files from FTP Server Using C# | Cybarlab Página 6 de 9

In this way we can transfer files from ftp server in C#. Hope you
all enjoy this article. We can also Download, Upload files from
SFTP Server in C# .

Related Posts

How to create a Bulk Data Insertion Generate Dynamic


DLL file in Visual from CSV to SQL Menu in ASP.NET
Studio Server C#

Comments

By Anitha R
how to use this program in windows application in
c#
on April 4, 2013 Reply

By Rezaul Hassan
This are C# code. So try to use it in your
Windows application. If you face any
problem let me know.
Thanks for comments.
on April 29, 2013 Reply

By Asava Samuel
All the other SFTP libraries that I have seen are
way overpriced. This is a good one:
https://www.kellermansoftware.com/p-41-net-sftp-
library.aspx
on April 8, 2013 Reply

By Rezaul Hassan
Thanks..
on April 29, 2013 Reply

http://cybarlab.com/download-upload-files-from-ftp-server-in-c-sharp 2016/07/18
Download, Upload,Delete Files from FTP Server Using C# | Cybarlab Página 7 de 9

By aarti mishra
I want to send files on other pc connected
through internet..How it would be possible?
on May 9, 2013 Reply

By Bal
Great Post very helpful !
on September 24, 2014 Reply

By rakesh
hey moving file code is not working please help
me
on January 17, 2015 Reply

By aa
moving code in the sense copy paste or
what?????
on April 7, 2016 Reply

By Toli
Hello,
I am trying to automatically download files from
ftp servers like ft.gnu.org and
http://ftp.mirror.apache,
I am able to traverse directories, but downloading
file is not letting me access site, and the ftp is
open does not require login.
please help
on April 22, 2016 Reply

By Ranjith
Hi All,
Can somebody help me , i have to download the
whole file present in the location where i
mentioned.
_FileName = “test1.csv”; this string cannot work
as it is only for a single file string
so can any one suggest me other option.
on July 15, 2016 Reply

http://cybarlab.com/download-upload-files-from-ftp-server-in-c-sharp 2016/07/18
Download, Upload,Delete Files from FTP Server Using C# | Cybarlab Página 8 de 9

Leave a Reply

Comment

Name

Email

Website

Post Comment

◾ C# ◾ API

◾ ASP.NET ◾ Interview

◾ ASP.NET MVC ◾ Mobile

◾ Entity Framework ◾ Visual Studio

◾ Software Engineering ◾ See more..

◾ About

◾ Contact

◾ Sitemap

◾ Terms of Use

http://cybarlab.com/download-upload-files-from-ftp-server-in-c-sharp 2016/07/18
Download, Upload,Delete Files from FTP Server Using C# | Cybarlab Página 9 de 9

◾ Privacy Policy

Cybarlab Copyright © 2016. Back to Top ↑

http://cybarlab.com/download-upload-files-from-ftp-server-in-c-sharp 2016/07/18

You might also like