Search - Articles
DevASP.NET for ASP.NET, VB.NET, XML and C# (C-Sharp) Developers Tuesday, March 03, 2009
Dev Articles
Search Directory
SQL Server

Using FtpWebRequest and FtpWebResponse classes using VB.NET in .NET Framework 2.0

Author: Aadil

In this article I’ll try to explain how you can use the newly added classes FtpWebRequest and FtpWebResponse to interact with any FTP server

In the new .NET Framework 2.0 Microsoft has added classes for operating with FTP servers. These classes are added in the System.Net namespace. You can download files from a remote FTP server using WebClient class. In this article I’ll explain how you can use these classes to get the list of directories and files present on a FTP server.


First you need to create an instance of the FtpWebRequest class and set the ftp address of the folder you want to get list from.


Dim fwr As FtpWebRequest = FtpWebRequest.Create(“”)


Then you need to set the user name and password for accessing the FTP server. There is a property called Credentials in the FtpWebRequest class that is of type ICredential and you can set it to an object of type NetworkCredential object like this.


fwr.Credentials = New NetworkCredential(“userName”, “password”)


Then you need to specify the Method of this class which is an instance of WebRequestMethods object like this.


fwr.Method = WebRequestMethods.Ftp.ListDirectory


After specifying these properties I’ll retrieve the list of files and folders using a Stream reader and display that in the console. The code for this is as follows.


Dim sr As New StreamReader(fwr.GetResponse().GetResponseStream())

Dim str As String = sr.ReadLine()

While Not str Is Nothing


  str = sr.ReadLine()

End While


sr = Nothing

fwr = Nothing


This will be the simplest example of using these classes to work with the file transfer protocol. Try to work with the code, change it and see what happens. Also find about all the members of this class from the MSDN library.

Article Comments
Good example, don't forget to explicitly use the import namespace

Posted on 12/26/2006 12:19:23 AM by Bill Rose

It was really a helpfull code

Posted on 1/5/2007 4:31:54 AM by shailendra kumar


I need to download collections of image from ftp server to local is possible,Can u plz help on this


Posted on 1/17/2007 6:03:46 AM by Govardhan

I'm am really new to VB.Net and class programming. I need an FTP Client that simply ftp's a file to a non-web server. Pretty simple yet so hard for me. What code would I need tweak in your sample in order for me to accomplish this task?


Posted on 2/16/2007 6:37:14 PM by Blake McKenna

Wonderful article! I ' am so happy!:O)

Posted on 4/13/2007 5:22:53 PM by Manuel Lenz

Hi, how can i view the images in the ftp site instead of only listing the directory? any great help will be appreaciate :)

Posted on 1/16/2008 9:53:38 PM by yeongland

Thank you very much this helped me a lot ; actually i was trying to transfera file and a folder throught ftp to a remote system where my application will download it .....

any more ideas plz ......

Posted on 2/29/2008 4:09:39 AM by jon

How do i List all directories on the root of ftp server?

Posted on 4/15/2008 5:12:46 AM by Rashed

Very, Very good article!!!!

Posted on 9/15/2008 10:26:02 AM by Paulo

It is a very useful code....
But i've a simple problem .. i want to establish connection with more than one server... How to do this using FTPWebrequest class?

I want to instantiate a class.. then using objects i want to connect to that server... please help??

Posted on 12/19/2008 1:34:02 AM by mainak

Thanks for sharing this. Your while condition is not giving exact result but when I use
while not sr.endofstream
every thing is working fine.

Posted on 12/22/2008 2:11:26 AM by pkhussain

Thank you very much its use full me, thanks a lot

Posted on 1/5/2010 6:39:13 AM by Raghunath

VEry good example helped me a lot

Posted on 3/2/2010 5:12:43 PM by chang

At last, light at the end of the tunnel. Thanks for simplifing things!

Posted on 3/11/2010 1:35:37 PM by Twiggy

Nice clear example. Just what I was looking for. Thanks.

Posted on 4/5/2010 6:13:57 PM by terp

Nice article

Posted on 6/11/2010 6:20:28 AM by Avinash Pohane

Great Article, but how do I list the contents of a subdirectory.
Getting the contents of the root directory give me the corrct listing (only the subdirectory) but if I add the subdirectory to the URI, I get an error:
The remote server returned an error: (450) File unavailable (e.g., file busy).

Many thanks if anyone is able to help.

My Code:
Private Function GetFTPList() As List(Of String)
Dim FileList As New List(Of String)
Dim URI As New UriBuilder("")
Dim UserName As String = "xxx"
Dim Password As String = "xxx"
Dim FTP As System.Net.FtpWebRequest = CType(System.Net.FtpWebRequest.Create(URI.Uri), System.Net.FtpWebRequest)
FTP.Credentials = New System.Net.NetworkCredential(UserName, Password)
FTP.KeepAlive = False
FTP.Method = System.Net.WebRequestMethods.Ftp.ListDirectory
Dim ResponseStream As New System.IO.StreamReader(FTP.GetResponse().GetResponseStream())
Dim ResponseLine As String = ResponseStream.ReadLine
While ResponseLine IsNot Nothing
ResponseLine = ResponseStream.ReadLine
End While
Return FileList
End Function

*if I leave out the 'subdirectory' part, it works fine, but only lists the root directory. :(

Posted on 6/11/2010 1:24:17 PM by ASPDave

Excellent, simple article. Thanks very much.

Posted on 7/9/2010 11:03:13 AM by Matt

Add Article Comment:
Name :
Email Address :
Comments :
<< Working with Reflection in VB.NET

Disclaimer - Privacy
© 2002-2017