C# Programming > Web

C# Download File FTP

c# ftp download

FTP Access with C#.NET

Thanks to the .NET Framework's System.Net namespace, all the basic functionality for accessing an FTP server is already built into C#. Using the FTPWebRequest class this article will go over two simple parts of accessing a FTP server. First how to get a list of files in the FTP server and then how do download a single file...

FTPWebRequest - Connecting to the Server with .NET

FtpWebRequest request = FtpWebRequest.Create([FTPString]) as FtpWebRequest;

request.Credentials = new NetworkCredential(username, password);
request.UsePassive = true;
request.UseBinary = true;
request.KeepAlive = true;

FtpWebResponse response = request.GetResponse() as FtpWebResponse;
Stream responseStream = response.GetResponseStream();

//... FTP commands
responseStream.Close();
response.Close(); //Closes the connection to the server

In a nutshell that is how to connect to a FTP Server with only C# code. No need for external libraries, cool huh? Depending on the commands you are going to send the [FTPString] string is going to vary as well as the Method property of the request object. So let's see what you can do with this.

List of Files

So let's say you want to get all the directories and files available inside the server. Let's first modify two of the C# lines:

FtpWebRequest request = FtpWebRequest.Create([FTPAddress]) as FtpWebRequest;
request.Method = WebRequestMethods.Ftp.ListDirectory;

Remember those are additions/modifications to the big chunck of source code above. The FTPAddress string in this case only has to be the address of the server, which makes sense since we want a list of files inside the whole server.

Now here is the C#.Net routine to read the directory:

List<string> files = new List<string>();
StreamReader reader = new StreamReader(responseStream);

while (!reader.EndOfStream)
{
     Application.DoEvents();
     files.Add(reader.ReadLine());
}
reader.Close();

Get File Size

So before you actually go ahead and begin downloading the file, first you want to ask the server how big the file is so you can keep track of the download percentage later on. Here are the two C# lines to modify this time:

FtpWebRequest request = FtpWebRequest.Create([FTPAddress]) as FtpWebRequest;
request.Method = WebRequestMethods.Ftp.ListDirectory;

Notice that the Create function needs to have the FTP address and the file name, so for example, it would be something like: ftp://myserver.com/myfile.txt. Once you have that, you can ask the FTP server for the filesize like so:

int dataLength = (int)request.GetResponse().ContentLength;

Downloading the File

At last it's time to download the file from the FTP server with pure C#.NET. By now you probably have guessed how easy this is going to be, the two source code lines to modify are very similar to the file size ones:

FtpWebRequest request = FtpWebRequest.Create([FTPAddress] + "/" + filename) as FtpWebRequest;
request.Method = WebRequestMethods.Ftp.DownloadFile;

The downloading C# routine is a very simple concept but the source code is a bit hefty due to the progress bar routine. Thus I'll save the actual source code for the project files you can download at the bottom of this page. But here is the main concept.

You basically want to have a while loop running infinitely, reading chuncks of the file at a time from the server, 1 to 2Kb is a good size. Inside that loop you need this line:

int bytesRead = reader.Read(buffer, 0, buffer.Length);

Where reader is the ResponseStream from the top of the page. The varible bytesRead will return how many bytes were actually downloaded, so if equals zero then the file download has been completed and the loop can be broken.

The Source Code

These three FTP commands can be perfectly integrated into one C# application to download files with FTP. Go ahead and download the sample application with its source code to see it all come together...

Back to C# Article List