Delete an FTP File

Source: Internet
Author: User
Tags ftp connection ftp file

FTP File Upload

FTP File Download

Delete an FTP File

Create directory for FTP File Operations

I have already introduced the use of FTP for upload and download. Next I will share with you the part for deleting files on the FTP server. With upload and download, deletion is essential.

Deleting is easier than uploading and downloading files. You only need to send a DELETE command to the FTP server.

The following is a complete example of the delete function:


         ///   <Summary>          ///  Delete files through FTP  ///   </Summary>          ///   <Param name = "ftppath">  FTP file path  </Param>          ///   <Param name = "userid">  FTP User Name  </Param>          ///  <Param name = "PWD">  FTP Password  </Param>          ///   <Param name = "FILENAME">  FTP file name  </Param>          ///   <Returns> </returns>          Public   String Deletefile ( String Ftppath, String Userid, String PWD,String  Filename ){  String Sret = "  Deleted successfully!  "  ; Ftpwebresponse respose = Null  ; Ftpwebrequest reqftp = Null  ; Stream localfile = Null  ; Stream = Null ;  Try  {  //  Create an ftpwebrequest object based on Uri Reqftp = (ftpwebrequest) ftpwebrequest. Create ( New Uri ( String . Format ( @"  {0} {1}  "  , Ftppath, filename )));  //  Provide account and password verification Reqftp. Credentials =New  Networkcredential (userid, PWD );  //  The default value "true" indicates that the FTP connection is not closed after the upload. Reqftp. keepalive = False  ;  //  Delete Reqftp. method = Webrequestmethods. FTP. deletefile; respose = (Ftpwebresponse) reqftp. getresponse ();}  Catch (Exception ex) {sret = Ex. Message ;}  Finally  {  //  Close connection and stream                  If (Respose! = Null  ) Respose. Close ();  If (Localfile! = Null  ) Localfile. Close ();  If (Stream! = Null  ) Stream. Close ();}  //  Return execution status              Return  Sret ;} 


It is not difficult to find thatCodeThis is much less than upload and download, which means it is simpler than the previous two functions. Although simple but equally important, FTP file operations usually occur at the same time, and work together to complete a series of work, and will continue to be updated in later blogs. Stay tuned!


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.