The framework provides us with various techniques to connect with FTP servers and interact with them. In this post, I will go over some of the ways and we will see how to add functionality to a FTP client.
Here is a limited class for our FTP. It has three methods: listFiles, makeDirectory and deleteDirectory.
1 Imports Microsoft.VisualBasic
2 Imports System.IO
3 Imports System.Net
4
5
6 Public Class ftpclientcode
7 Protected myftprequest As FtpWebRequest
8 Protected myftpresponse As FtpWebResponse
9 Private _myserver As String
10 Private _myuser As String
11 Private _mypw As String
12
13 Public Sub New(ByVal myserver As String, ByVal myuser As String, ByVal mypw As String)
14 _myserver = myserver
15 _myuser = myuser
16 _mypw = mypw
17 myftprequest = WebRequest.Create(_myserver)
18 myftprequest.Credentials = New NetworkCredential(_myuser, _mypw)
19
20 End Sub
21
22
23 Public Function ListFiles() As Stream
24 myftprequest.Method = WebRequestMethods.Ftp.ListDirectory
25 myftpresponse = myftprequest.GetResponse()
26 Return myftpresponse.GetResponseStream()
27
28 End Function
29
30 Public Sub MakeDirectory(ByVal mydirname As String)
31
32 myftprequest.Method = WebRequestMethods.Ftp.MakeDirectory
33 myftpresponse = myftprequest.GetResponse()
34
35 End Sub
36
37 Public Shared Sub Close()
38 ' myftpresponse.Close()
39
40 End Sub
41
42 Public Sub DeleteDirectory(ByVal mydirname As String)
43 myftprequest.Method = WebRequestMethods.Ftp.RemoveDirectory
44 myftpresponse = myftprequest.GetResponse()
45
46 End Sub
47
48
49 End Class
And here is the Default.aspx page to work with our class
1 Imports System.Net
2 Imports System.IO
3
4 Partial Class _Default
5 Inherits System.Web.UI.Page
6
7 Private servername As String = "localhost"
8 Private myusername As String = "rafayali"
9 Private mypassword As String = "******"
10
11
12 Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
13 Dim myftp As New ftpclientcode("ftp://" & servername, myusername, mypassword)
14 Label1.Text = ""
15 Dim datastream As Stream = myftp.ListFiles()
16 Dim reader As New StreamReader(datastream)
17 While Not reader.EndOfStream
18 'Response.Write(reader.ReadLine() & "<br/>")
19 Label1.Text = Label1.Text & " ? " & reader.ReadLine() & "<br/>"
20 End While
21 'myftp.Close()
22 End Sub
23
24 Protected Sub createdir_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles createdir.Click
25 Panel1.Visible = False
26 Panel2.Visible = False
27 Panel3.Visible = True
28 TextBox1.Focus()
29 End Sub
30
31 Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles createbutton.Click
32 Panel3.Visible = False
33 Panel1.Visible = True
34 Panel2.Visible = True
35 Dim myftp As New ftpclientcode("ftp://" & servername & "/" & TextBox1.Text, myusername, mypassword)
36 Try
37 myftp.MakeDirectory(TextBox1.Text)
38 Page_Load(sender, e)
39 Catch ex As Exception
40 Response.Write(ex.Message.ToString())
41 Finally
42 'myftp.Close()
43 End Try
44 End Sub
45
46 Protected Sub cancelbutton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles cancelbutton.Click, Button2.Click
47 Panel1.Visible = True
48 Panel2.Visible = True
49 Panel3.Visible = False
50 Panel4.Visible = False
51 End Sub
52
53 Protected Sub removedir_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles removedir.Click
54 Panel1.Visible = False
55 Panel2.Visible = False
56 Panel4.Visible = True
57 TextBox2.Focus()
58 End Sub
59
60 Protected Sub Button1_Click1(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
61 Panel4.Visible = False
62 Panel1.Visible = True
63 Panel2.Visible = True
64
65 Dim myftp As New ftpclientcode("ftp://" & servername & "/" & TextBox2.Text, myusername, mypassword)
66 Try
67 myftp.DeleteDirectory(TextBox2.Text)
68
69 Catch ex As Exception
70 Response.Write(ex.Message.ToString())
71
72 End Try
73 Page_Load(sender, e)
74 End Sub
75
76 Protected Sub Page_Unload(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Unload
77
78
79 End Sub
80 End Class
81