Building FTP Services Using .NET 2.0
Pages: 1, 2
Uploading a Text File
To upload a text file to an FTP server:
- Create an instance of the
FtpWebRequestclass using theCreate()method of theWebRequestclass. TheCreate()method takes in a URI parameter (containing the path for the file to be uploaded). - Set the command to be sent to the FTP server using the
Methodproperty of theFtpWebRequestclass; in this case this command isUploadFile. - Specify the login credential to the FTP server.
- Use a
StreamReaderobject to read the contents of the text file to be uploaded. - Retrieve the stream used to upload data to an FTP server using the
GetRequestStream()method of theFtpWebRequestclass. - Write the content of the text file to be uploaded into the stream.
- Obtain the response from the FTP server using the
GetResponse()method from theFtpWebRequestclass.
Try
Dim filename As String = ftpURI & "test.txt"
Dim ftpReq As FtpWebRequest = WebRequest.Create(filename)
ftpReq.Method = WebRequestMethods.Ftp.UploadFile
ftpReq.Credentials = New NetworkCredential("anonymous", "password")
Dim stream As StreamReader = New StreamReader("C:\test.txt")
Dim b() As Byte = _
System.Text.Encoding.UTF8.GetBytes(stream.ReadToEnd())
stream.Close()
ftpReq.ContentLength = b.Length
Dim s As Stream = ftpReq.GetRequestStream()
s.Write(b, 0, b.Length)
s.Close()
Dim ftpResp As FtpWebResponse = ftpReq.GetResponse()
MsgBox(ftpResp.StatusDescription)
Catch ex As Exception
MsgBox(ex.ToString)
End Try
Removing a File
To remove a file from an FTP server:
- Create an instance of the
FtpWebRequestclass using theCreate()method of theWebRequestclass. TheCreate()method takes in a URI parameter (containing the path for the file to be removed). - Set the command to be sent to the FTP server using the
Methodproperty of theFtpWebRequestclass; in this case this command isDeleteFile. - Specify the login credential to the FTP server.
- Obtain the response from the FTP server using the
GetResponse()method from theFtpWebRequestclass.
Try
Dim filename As String = ftpURI & "PING.bmp"
Dim ftpReq As FtpWebRequest = WebRequest.Create(filename)
ftpReq.Method = WebRequestMethods.Ftp.DeleteFile
ftpReq.Credentials = New NetworkCredential("anonymous", "password")
Dim ftpResp As FtpWebResponse = ftpReq.GetResponse
MsgBox(ftpResp.StatusDescription)
Catch ex As Exception
MsgBox(ex.ToString)
End Try
Making a New Directory
To create a new directory on an FTP server:
- Create an instance of the
FtpWebRequestclass using theCreate()method of theWebRequestclass. TheCreate()method takes in a URI parameter (containing the path of the new directory to create). - Set the command to be sent to the FTP server using the
Methodproperty of theFtpWebRequestclass; in this case this command isMakeDirectory. - Specify the login credential to the FTP server.
- Obtain the response from the FTP server using the
GetResponse()method from theFtpWebRequestclass.
Try
Dim filename As String = ftpURI & "User1"
Dim ftpReq As FtpWebRequest = WebRequest.Create(filename)
ftpReq.Method = WebRequestMethods.Ftp.MakeDirectory
ftpReq.Credentials = New NetworkCredential("anonymous", "password")
Dim ftpResp As FtpWebResponse = ftpReq.GetResponse
MsgBox(ftpResp.StatusDescription)
Catch ex As Exception
MsgBox(ex.ToString)
End Try
Figure 3 shows the contents of my FTP directory after creating the new directory User1.

Figure 3. The contents of my FTP directory after making a new directory
Viewing Directory Listing
To view the directory listing of an FTP account:
- Create an instance of the
FtpWebRequestclass using theCreate()method of theWebRequestclass. TheCreate()method takes in a URI parameter (containing the full FTP path). - Set the command to be sent to the FTP server using the
Methodproperty of theFtpWebRequestclass; in this case this command isListDirectoryDetails. - Specify the login credential to the FTP server.
- Obtain the response from the FTP server using the
GetResponse()method from theFtpWebRequestclass. - Retrieve the stream that contains response data sent from an FTP server using the
GetResponseStream()method from theFtpWebResponseclass.
Note that you can use a StreamReader object to read the directory listing.
Try
Dim ftpReq As FtpWebRequest = WebRequest.Create(ftpURI)
ftpReq.Method = WebRequestMethods.Ftp.ListDirectoryDetails
ftpReq.Credentials = New NetworkCredential("anonymous", "password")
Dim FTPResp As FtpWebResponse = ftpReq.GetResponse
Dim ftpRespStream As Stream = FTPResp.GetResponseStream
Dim reader As StreamReader
reader = New StreamReader(ftpRespStream, System.Text.Encoding.UTF8)
Console.WriteLine(reader.ReadToEnd)
Catch ex As Exception
MsgBox(ex.ToString)
End Try
Depending on how the FTP server is configured, you will see something like this:
(MS-DOS Directory Listing Style)
10-19-06 11:14AM 19387 phidgetRFID.jpg
12-08-06 02:01PM 242490 PING.bmp
12-08-06 10:32AM 5 test.txt
12-08-06 02:26PM <DIR> User1
or:
(UNIX Directory Listing Style)
-r-xr-xr-x 1 owner group 19387 Oct 19 11:14 phidgetRFID.jpg
-rwxrwxrwx 1 owner group 242490 Dec 8 14:01 PING.bmp
-r-xr-xr-x 1 owner group 5 Dec 8 10:32 test.txt
drwxrwxrwx 1 owner group 0 Dec 8 14:26 User1
Note that you need to manually parse the directory listing to differentiate the directories from the files. The FTP classes in .NET 2.0 does not provision for this.
Removing a Directory
To remove a directory on an FTP server:
- Create an instance of the
FtpWebRequestclass using theCreate()method of theWebRequestclass. TheCreate()method takes in a URI parameter (containing the path of the new directory to remove). - Set the command to be sent to the FTP server using the
Methodproperty of theFtpWebRequestclass; in this case this command isRemoveDirectory. - Specify the login credential to the FTP server.
- Obtain the response from the FTP server using the
GetResponse()method from theFtpWebRequestclass.
Try
Dim filename As String = ftpURI & "User1"
Dim ftpReq As FtpWebRequest = WebRequest.Create(filename)
ftpReq.Method = WebRequestMethods.Ftp.RemoveDirectory
ftpReq.Credentials = New NetworkCredential("anonymous", "password")
Dim ftpResp As FtpWebResponse = ftpReq.GetResponse
MsgBox(ftpResp.StatusDescription)
Catch ex As Exception
MsgBox(ex.ToString)
End Try
Summary
While the new FTP classes in .NET 2.0 make transferring files between clients and FTP servers easy, they does have their shortcomings. For example, you need to maintain your own state, as each request requires a full valid path. This is evident if you try to implement a full-blown FTP client that allows users to navigate directories. In this case, you need to know exactly which directory the user is in before you can get a listing of files in that particular directory (since the FTP classes do not remember the current directory). However, if all you need is access to an FTP server for simple file transfer, the new managed classes in .NET 2.0 are worth a look.
Wei-Meng Lee (Microsoft MVP) http://weimenglee.blogspot.com is a technologist and founder of Developer Learning Solutions http://www.developerlearningsolutions.com, a technology company specializing in hands-on training on the latest Microsoft technologies.
Return to the Windows DevCenter.
-
Building FTP Services
2009-02-02 07:32:41 DeeInSimsbury [View]
-
Building FTP Services
2009-11-18 06:54:47 tapsa_gbg [View]
-
Delete/ Remove ftp directory using c#
2008-05-06 22:55:24 umesh.chape [View]
-
Invalid characters in FTP
2008-01-06 20:24:55 coolninja01 [View]
-
Thanks for this Artical
2007-09-19 03:21:33 SantoshLonkar [View]
-
Example for .NET C++
2007-09-09 07:07:29 aghochikayn [View]
-
Example for .NET C++
2008-11-23 12:44:08 Stefan W [View]
-
What about async ftp functions
2007-05-17 10:53:32 lorenzosjb [View]
-
file exists
2007-03-13 11:23:04 tuzojazz [View]
-
System.Net.WebException
2007-03-06 11:29:45 tuzojazz [View]
-
1000 Thanks
2007-03-02 05:58:46 mwbrady68 [View]
-
thanks for this article
2006-12-13 04:57:56 MrUbu [View]

