-1

I am trying to delete all the files in a folder via FTP. Below is the code I am trying.

Files is an array of strings each one if the name of a file in the folder with its extension.

When I run it I get an a reply of 206 but when I look in the folder all the files remain. I tried variations of the code below, including adding a delay, but still cannot delete the files. What have I missed?

 foreach (var FileName2 in Files)
     {
       if (File.Exists(txtbx_save_backup_to.Text + "/" + FileName2))
                {
                    FtpWebRequest Delrequest = (FtpWebRequest)WebRequest.Create(ftp_address + "/Temp/Backup/" + FileName2);
                   Delrequest.Credentials = new NetworkCredential(username, password);
                   Delrequest.Method = WebRequestMethods.Ftp.DeleteFile;

                    Task.Delay(1000);

                    using (FtpWebResponse response2 = (FtpWebResponse)request.GetResponse())
                    {
                        rchtxtbx_backup_comms.AppendText("Deleted File, status " + response2.StatusDescription + "\r");
                        rchtxtbx_backup_comms.ScrollToCaret();
                    }
                }
            }
user3884423
  • 556
  • 1
  • 5
  • 20
  • Does [this](https://stackoverflow.com/questions/4797508/how-can-ftpclient-delete-a-directory) answers your question? – Rohan Bari Apr 22 '20 at 20:22
  • Show us a [log file](https://stackoverflow.com/q/9664650/850848). + Can you delete those files sucessfuly using any (GUI/commandline) FTP client? – Martin Prikryl Apr 23 '20 at 06:42

1 Answers1

0

The comments above gave me the clue I needed in a roundabout way so thanks to you for answering.

I had missed out the part to "action" the delete request. So I added the following and now it works.

WebResponse GetResponse = Delrequest.GetResponse();
Stream GResponseStream = GetResponse.GetResponseStream();

I removed the wait and the complete code is now

foreach (var FileName2 in Files)
            {
                if (File.Exists(txtbx_save_backup_to.Text + "/" + FileName2))
                {
                    FtpWebRequest Delrequest = (FtpWebRequest)WebRequest.Create(ftp_address + "/Temp/Backup/" + FileName2);
                   Delrequest.Credentials = new NetworkCredential(username, password);
                   Delrequest.Method = WebRequestMethods.Ftp.DeleteFile;

                    //Action request
                    WebResponse GetResponse = Delrequest.GetResponse();
                    Stream GResponseStream = GetResponse.GetResponseStream();

                    using (FtpWebResponse response2 = (FtpWebResponse)request.GetResponse())
                    {
                        rchtxtbx_backup_comms.AppendText("Deleted File, status " + response2.StatusDescription + "\r");
                        rchtxtbx_backup_comms.ScrollToCaret();
                    }

                    GResponseStream.Close();
                }
            }
user3884423
  • 556
  • 1
  • 5
  • 20
  • OK, so the actual problem is that the `request.GetResponse()` should be `Delrequest.`, not `request.` – Your current code will still display status of some other request. – Martin Prikryl Apr 23 '20 at 08:10