locked
Delete a file from an FTP server RRS feed

  • Question

  • I have the code to download and upload a file to and from a FTP server fine. What I am not sure of is how to delete a file from an FTP server. I figured I could try to download a file, and if it downloads successfully, then to delete it because the download method would have all of the information needed of what the filename is and where it is located etc... below is the code for my download method in case it matters:

    public void Download(string downloadUrl, string user, string pswd, string downloadLocation, Boolean multfiles)
    {
      if (multfiles == false)
      {
        //write to email transfer number
        transfer++;
        errors = errors +
    "\r\n\r\nTransfer #" + transfer;
      }
      Stream responseStream = null;
      FileStream fileStream = null;
      StreamReader reader = null;
      try
      {
        FtpWebRequest downloadRequest = (FtpWebRequest)WebRequest.Create(downloadUrl);
        // Since the FTP you are downloading from is secure, send
        // in user name and password to be able to access the FTP files
        if (user != "" && pswd != "" && user != null && pswd != null)
        {
          ICredentials credentials = new NetworkCredential(user, pswd);
         downloadRequest.Credentials = credentials;
        }
        FtpWebResponse downloadResponse = (FtpWebResponse)downloadRequest.GetResponse();
        responseStream = downloadResponse.GetResponseStream();
        string fileName = Path.GetFileName(downloadRequest.RequestUri.AbsolutePath);
        if (fileName.Length == 0)
        {
          reader =
    new StreamReader(responseStream);
          MessageBox.Show(reader.ReadToEnd());
        }
        else
        {
          //specific path of where you want to download the document to
          fileStream = File.Create(downloadLocation + "/" + fileName);
          byte[] buffer = new byte[1024];
          int bytesRead;
          while (true)
          {
            bytesRead = responseStream.Read(buffer, 0, buffer.Length);
            if (bytesRead == 0)
              break;
            fileStream.Write(buffer, 0, bytesRead);
          }
          //download completed successfully, so show it in Form1
          JobsCompleted.Items.Add("**Download Locally**");
          JobsCompleted.Items.Add(
    "File: " + fileName);
          JobsCompleted.Items.Add(
    "Location: " + downloadLocation);
          JobsCompleted.Items.Add
            (
    "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
          errors = errors +
    "\r\nSuccess: download file: " + downloadUrl;
          //WOULD WANT TO ADD THE DELETE PART HERE BECAUSE THE FILE DOWNLOADED
           
    SUCCESSFULLY
        }
      }
      catch (UriFormatException ex)
      {
        ErrorsHappened.Items.Add(
    "User (Download): " + user);
        ErrorsHappened.Items.Add(
    "File: " + downloadUrl);
        ErrorsHappened.Items.Add(
    "Error: " + ex.Message);
        ErrorsHappened.Items.Add
          (
    "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
        Email.logger("User (Download): " + user);
        Email.logger("File: " + downloadUrl);
        Email.logger("Error: " + ex.Message);
        errors = errors +
    "\r\nError: download file : " + downloadUrl;
        errors = errors +
    "\r\nError: Message : " + ex.Message;
      }
      catch (WebException ex)
      {
        ErrorsHappened.Items.Add(
    "User (Download): " + user);
        ErrorsHappened.Items.Add(
    "File: " + downloadUrl);
        ErrorsHappened.Items.Add(
    "Error: " + ex.Message);
        ErrorsHappened.Items.Add
          (
    "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
        Email.logger("User (Download): " + user);
        Email.logger("File: " + downloadUrl);
        Email.logger("Error: " + ex.Message);
        errors = errors +
    "\r\nError: download file : " + downloadUrl;
        errors = errors +
    "\r\nError: Message : " + ex.Message;
      }
      catch (IOException ex)
      {
        ErrorsHappened.Items.Add(
    "User (Download): " + user);
        ErrorsHappened.Items.Add(
    "File: " + downloadUrl);
        ErrorsHappened.Items.Add(
    "Error: " + ex.Message);
        ErrorsHappened.Items.Add
          (
    "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
        Email.logger("User (Download): " + user);
        Email.logger("File: " + downloadUrl);
        Email.logger("Error: " + ex.Message);
        errors = errors +
    "\r\nError: download file : " + downloadUrl;
        errors = errors +
    "\r\nError: Message : " + ex.Message;
      }
      finally
      {
        if (reader != null)
          reader.Close();
        else if (responseStream != null)
          responseStream.Close();
        if (fileStream != null)
          fileStream.Close();
      }
    }

    Thanks!

    ~Lauren

    Monday, June 12, 2006 1:54 PM

Answers

  • To take the example from the MSDN page on the FtpWebRequest class:

    public static bool DeleteFileOnServer(Uri serverUri)
    {
        // The serverUri parameter should use the ftp:// scheme.
        // It contains the name of the server file that is to be deleted.
        // Example: ftp://contoso.com/someFile.txt.
        //
       
        if (serverUri.Scheme != Uri.UriSchemeFtp)
        {
            return false;
        }
        // Get the object used to communicate with the server.
        FtpWebRequest request = (FtpWebRequest)WebRequest.Create(serverUri);
        request.Method = WebRequestMethods.Ftp.DeleteFile;

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

    Monday, June 12, 2006 3:50 PM
    Moderator

All replies

  • To take the example from the MSDN page on the FtpWebRequest class:

    public static bool DeleteFileOnServer(Uri serverUri)
    {
        // The serverUri parameter should use the ftp:// scheme.
        // It contains the name of the server file that is to be deleted.
        // Example: ftp://contoso.com/someFile.txt.
        //
       
        if (serverUri.Scheme != Uri.UriSchemeFtp)
        {
            return false;
        }
        // Get the object used to communicate with the server.
        FtpWebRequest request = (FtpWebRequest)WebRequest.Create(serverUri);
        request.Method = WebRequestMethods.Ftp.DeleteFile;

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

    Monday, June 12, 2006 3:50 PM
    Moderator
  • Thank you Brendan! It is working perfectly now.

    ~Lauren  

    Monday, June 12, 2006 4:05 PM