Following shows usage of the above class file. The class file is named ftpclientcode.
'The following is code to delete directory on FTP server. It executes when button1 is clicked.
Protected Sub Button1_Click1(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
Panel4.Visible = False
Panel1.Visible = True
Panel2.Visible = True
Dim myftp As New ftpclientcode("ftp://" & servername & "/" & TextBox2.Text, myusername, mypassword)
Try
myftp.DeleteDirectory(TextBox2.Text)
Catch ex As Exception
Response.Write(ex.Message.ToString())
End Try
Page_Load(sender, e)
End Sub
'Following is code to create directory on FTP Server
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles createbutton.Click
Panel3.Visible = False
Panel1.Visible = True
Panel2.Visible = True
Dim myftp As New ftpclientcode("ftp://" & servername & "/" & TextBox1.Text, myusername, mypassword)
Try
myftp.MakeDirectory(TextBox1.Text)
Page_Load(sender, e)
Catch ex As Exception
Response.Write(ex.Message.ToString())
Finally
'myftp.Close()
End Try
'The following code executes on Page_Load and instantiates ftpclientcode class and logs into the FTP server and lists all the files on FTP server
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim myftp As New ftpclientcode("ftp://" & servername, myusername, mypassword)
Label1.Text = ""
Dim datastream As Stream = myftp.ListFiles()
Dim reader As New StreamReader(datastream)
While Not reader.EndOfStream
'Response.Write(reader.ReadLine() & "<br/>")
Label1.Text = Label1.Text & " ª " & reader.ReadLine() & "<br/>"
End While
'myftp.Close()
End Sub
'Following are class level variables declared (outside of any subs or function routines)
Private servername As String = "localhost"
Private myusername As String = "rafayali"
Private mypassword As String = "29jan1980"
1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48: 49: 50: 51: 52: 53: 54: 55: 56: 57: 58: 59: 60: 61: 62: 63: 64: 65: 66: 67: 68: 69: 70: 71: 72:





by: rafayaliPosted on 2008-06-18 at 17:27:50ID: 21818398
Here is some sample code from a ftp project that I was working on. It has some methods to list directories, create directories and delete directories. If you can figure out how to upload a file from the sample I have pasted, thats fine. Other wise let me know and I will show you (as a code example) how to upload a file using FTP in .NET.
Here is the sample code. It should be pretty straight forward to figure out:
Have a look at two posts below: First post is the class file, second is how class is used in the default.aspx page.
Select allOpen in new window