Delete a file from the ftp server

Dan181920

Active member
Joined
Jun 28, 2011
Messages
27
Programming Experience
Beginner
Hello,

I am attempting to add functionality to my VB.Net and ASP.Net program that allows a user to enter the name of a file (and its extension) in to a textbox and press on a delete button which will then delete the file from my FTP site.

Below is my code so far:

VB.NET:
Protected Sub Button6_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button6.Click
                Dim ftp As FtpWebRequest = DirectCast(WebRequest.Create("[URL]ftp://xx.xx.x.xx/In/[/URL]" + TextBox2.Text), FtpWebRequest)
                If TextBox2.Text = "" Then
                    MsgBox("A file needs to be selected!")
                End If
                If TextBox1.Text <> "" Then
                    Try
                        [URL="ftp://ftp.Credentials"]ftp.Credentials[/URL] = New System.Net.NetworkCredential("user", "password")
                        [URL="ftp://ftp.Method"]ftp.Method[/URL] = WebRequestMethods.Ftp.DeleteFile
                        Dim ftpResponse As FtpWebResponse = CType([URL="ftp://ftp.GetResponse"]ftp.GetResponse[/URL](), FtpWebResponse)
                        ftpResponse = [URL="ftp://ftp.GetResponse"]ftp.GetResponse[/URL]()
                        ftpResponse.Close()
                    Catch ex As Exception
                    End Try
                End If
            End Sub

I have tried doing it with this code, but when I go back to the FTP site, the file still remains.

Any help and advice will be greatly appreciated.

Many Thanks,

Dan
 

Dan181920

Active member
Joined
Jun 28, 2011
Messages
27
Programming Experience
Beginner
Hi John,

I have got it working now.

There is a bit of my code that says
VB.NET:
 If TextBox1.Text <> "" Then

It should have been:
VB.NET:
 If TextBox2.Text <> "" Then

Thank you very much for all your help,

Dan
 

JohnH

VB.NET Forum Moderator
Staff member
Joined
Dec 17, 2005
Messages
15,439
Location
Norway
Programming Experience
10+
About control names, 'textbox1' and textbox2' is something you are supposed to change to something meaningful. If the meaning is really one/first and two/second they should be called FirstTextBox or TextBoxOne and the like, but from the usage here one of them should probably be FilenameTextBox. It will make code much easier to read and debug.
MsgBox("A file needs to be selected!")
About choice of controls, you said it here. Though using MsgBox function in ASP.Net makes no sense at all, the message is right. For the FTP delete operation to be valid the file must exist on FTP server. The list of files is readily available and you should provide this list so the user can select one, and not having to write something in a textbox. Various list controls are available for ASP.Net delevelopment and I recommend you explore these options.
 

Dan181920

Active member
Joined
Jun 28, 2011
Messages
27
Programming Experience
Beginner
What sort of list controls do you mean?

I used the textbox method, so I could get a basic operation working and then build on it.

Dan
 

Dan181920

Active member
Joined
Jun 28, 2011
Messages
27
Programming Experience
Beginner
The list controls I have seen in design view all seem to relate to database records though.

I am starting to get a little bit confused.
 
Top Bottom