Friday, January 18, 2008

Get file list from the server using ftp web request in C#

Sometimes developers need to get a list of all the files on a server, and this needs to be done using ftp. Microsoft .Net provides classes to handle ftp requests. I'll show you some other functions performed using these classes in my later posts. Today, I'll focus only on getting a list of files from ftp server. I'll use FTPWebRequest class to fetch the list of files, and a StreadReader class to read the stream returned by FTPWebRequest method. Code is in C# (C-Sharp).

First of all you need to include two namespaces:

using System.Collections.Generic;
using System.Net;

System.Net namespace provides FTPWebRequest class to make a web request to an ftp server.

Now, declare a generic list of strings to keep the list of the fetched files:

List strList = new List();

In order to understand how to use generic list you can view my following post:
Using a generic list of strings or other class objects

Declare an object of type FTPWebRequest, and create an instance using the ftp server IP.
Here ftpServerIP is the IP of FTP server from where you want to fetch the list of files.

FtpWebRequest fwr = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://" + ftpServerIP));

FTPWebRequest method requires proper credentials to authenticate the request. In the statement below ftpUserID and ftpPassword variables serve this purpose.

fwr.Credentials = new NetworkCredential(ftpUserID, ftpPassword);

WebRequestMethod in the following code line tells the FTP Web Request object the operation you want to perform. In our current scenario we want to get the list of all the files in the root directory at the ftp server, so we'll use ListDirectory method type.

fwr.Method = WebRequestMethods.Ftp.ListDirectory;

Now, we'll create an object of type StreamReader as given below:

StreamReader sr = new StreamReader(fwr.GetResponse().GetResponseStream());

The important thing to understand here is 'fwr.GetResponse().GetResponseStream()'. As you already know that fwr is an FTPWebRequest type object and GetResponse method of this object returns an object of type WebResponse, so call an other method named GetResponseStream to create a StreamReader object. It shouldn't be confusing; nevertheless I'll discuss streams and related stuff in some later posts.

Okay, fellas! Now, we're ready to reap the benefits of all the labor we have done until now. :-)

We'll read the stream one line at a time - which will be a string - and we'll add these strings in the strList collection.

string str = sr.ReadLine();
while (str != null)
{
strList.Add(str);
str = sr.ReadLine();
}

Remember that the stream we received from the ftp server contained a list of files that is one file in one line!

So, we're almost done with the task we were supposed to do, but don't forget to impress your development manager or technical lead by the following statements - be smart and don't leave the labor for the garbage collector!

sr.Close();
sr = null;
fwr = null;

2 comments:

RRave said...
This comment has been removed by a blog administrator.
vivek kumar singh said...

Nice Post.....Its very helpfull all of that start SSIS