Question Deleting a file from an FTP site (am I doing it correctly)

solorize

Member
Joined
Feb 22, 2012
Messages
7
Programming Experience
Beginner
Hi,


I currently have the following portion of code below, which deletes a file
"solarradiationchart.png" from an FTP site, from within my program.

But a couple of users are having problems with my program which I think I have
pinpointed to this portion of code, as the other code that runs after
it does not get processed.

Therefore I have two questions below, which I hope someone can answer.

1.
Is there additional code that I will need to add to handle if the file does not exist
on the FTP server? or is the code OK as it is?

2.
Also should I be using "response.Dispose()" or something similar to fully close and exit out cleanly?
I did try "response.Dispose()" but VB would not allow it.

As I am still very much learning on how to working with FTP sites from within VB.NET, therefore
if it is possible could someone show me what code I should add and where, it would be much appreciated.

VB.NET:
If CheckBox2.Checked = False Then

    'Delete "solarradiationchart.png" on FTP server if it exists
    Dim request As System.Net.FtpWebRequest = DirectCast(System.Net.WebRequest.Create("ftp://" + FTP_URL + "solarradiationchart.png"), System.Net.FtpWebRequest)
    request.Credentials = New System.Net.NetworkCredential(FTP_UserName, FTP_Password) '// put username and password in here.
    request.Method = System.Net.WebRequestMethods.Ftp.DeleteFile

    Dim response As FtpWebResponse = request.GetResponse
    Console.WriteLine("Delete status: {0}", response.StatusDescription)
    response.Close()

End If

FYI.
I am also going to add a "Try and Catch" to the code to see if I can find out
what the error is which will also hopefully help.
 
Back
Top