Sei sulla pagina 1di 9

Download, Upload,Delete Files from FTP Server Using C# | Cybarlab

Pgina 1 de 9

Cybarlab
Home

Programming

Services

Reporting

Framework
Interview

Database

Scripting

HTML/CSS

All Post

09 Mar 2013
Search this site...

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

Search

FTP Server Using C#

Download, Upload,Delete
Files from FTP Server Using
C#

Popular Posts Recent Posts

Posted in ASP.NET, C# By Rashed On March 9, 2013

Find a string inside an


entire database

CRUD operations in
AngularJS and Web API
SQL Queries Optimization
Tips

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 pgina foi classificada como "Not Categorized" e o se

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


C#

Para
ter cincia
Check File Existence in the FTP
Server
in C#de quais as categorias de pginas (ou sites) bloqueadas, consulte o
Se voc considera a categorizao 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

Pgina 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
string
string
string
List F

_ftpURL = "testftp.com";
_UserName = "admin";
_Password = "admin123";
_ftpDirectory = "Receipts";

//Host URL or address of the FTP server


//User Name of the FTP server
//Password of the FTP server
//The directory in FTP server where the files a

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

string _ftpURL = "testftp.com";


string _UserName = "admin";
string _Password = "admin123";
string _ftpDirectory = "Receipts";
string _FileName = "test1.csv";
string _LocalDirectory = "D:\\FilePuller";
DownloadFile(_ftpURL, _UserName, _Password,

Pgina 3 de 9

//Host URL or address of the FTP server


//User Name of the FTP server
//Password of the FTP server
//The directory in FTP server where the
//File name, which one will be download
//Local directory where the files will
_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
string
string
string
string
string

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

_UserName = "admin";
//User Name of the FTP server
_Password = "admin123";
//Password of the FTP server
_ftpDirectory = "Receipts";
//The directory in FTP server where the
name, which one will be uploaded
_FileName = "test1.csv";
//File
_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

Pgina 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";
string _UserName = "admin";
string _Password = "admin123";
string _ftpDirectory = "Receipts";
string _FileName = "test1.csv";
string _ftpDirectoryProcessed = "Done";
MoveFile(_ftpURL, _UserName, _Password,

//Host
URL or address of the FTP server
//User Name of the FTP server
//Password of the FTP server
//The directory in FTP server where the fil
//File name, which one will be uploaded
//The directory in FTP server where the fil
_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

Pgina 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

Pgina 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-sftplibrary.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

Pgina 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

Pgina 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

Pgina 9 de 9

Privacy Policy

Cybarlab Copyright 2016.

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

Back to Top

2016/07/18

Potrebbero piacerti anche