Practical Approach of Deleting Files From FTP Path

Introduction

Now let's see how to delete a file from the FTP path.

Step 1: Used Namespaces:

using System;
using System.IO;
using
System.Net;
using
System.Text;
using
System.Web;

Step 2: Usage:

DeleteFtpFile ("FileName","ftp://208.43.121.51/", "Username", "Password");

Step 3: Creating FTP request by specifying file name and path:

public
static FtpWebRequest CreateFTPRequest(string fullFtpPath, string fileName)
{

    return (FtpWebRequest)FtpWebRequest.Create(new Uri(fullFtpPath + "/" + fileName))
}

Step 4: Setting user name and password for FTP path:

public
static NetworkCredential CreateNetworkCredentials(string ftpUserID, string ftpPassword)
{

    return
new NetworkCredential(ftpUserID, ftpPassword);
}

Step 5: Getting current response to if any error occurs:

public
static HttpResponse GetHttpResponse()
{
    return HttpContext.Current.Response;
}

Step 6: Setting of FTP request:

public static FtpWebRequest GetFTPWebRequest(string fullFtpPath,

string ftpUserID, string ftpPassword)

{

    FtpWebRequest reqFTP = CreateFTPRequest(fullFtpPath, fileName);

    reqFTP.Credentials = CreateNetworkCredentials(ftpUserID, ftpPassword);

    reqFTP.Method = WebRequestMethods.Ftp.DeleteFile;

 

    reqFTP.UseBinary = true; // Specify the data transfer type.

    return reqFTP;

}

Step 7: Used to delete the file from the FTP path:

//Getting response from Request.

FtpWebResponse
response = (FtpWebResponse)reqFTP.GetResponse();
//Getting FileStream from the response.

Stream
ftpStream = response.GetResponseStream();

//Deletes the File from the FTP path.

DeleteFTPFile
(response);

ftpStream.Close();

response.Close();
   

Step 8: Deleting mechanism:

public
static void DeleteFTPFile(FtpWebResponse response)
{
      try
     
{
              string result = string.Empty;

              //Getting response form request.

              Stream datastream = response.GetResponseStream();

                       

              //Reading the Stream to the end of file.

              StreamReader sr = new StreamReader(datastream);

              result = sr.ReadToEnd();

              sr.Close();

              datastream.Close();

              response.Close();

       }

       catch (Exception ex)

       {

            GetHttpResponse().Write(ex.Message + " Delete Error");

       }

}

Step 9: COPY & PASTE code snippet:

using System;

using System.Collections.Generic;

using System.IO;

using System.Net;

using System.Text;

 

namespace ApplicationClassLibrary

{

    class FTPDelete

    {

        //Used to Delete File from FTP

        public static void DeleteFtpFile(string fileName, string fullFtpPath, string ftpUserID, string ftpPassword)

        {

            FtpWebRequest reqFTP;

            try

            {

                // Creating request to delete the file form FTP.

                reqFTP = CreateFTPRequest(fullFtpPath, fileName);

 

                //Setting FTP method, which helps to delete file.

                reqFTP.Method = WebRequestMethods.Ftp.DeleteFile;

 

                //Specifies the Data type for transfer

                reqFTP.UseBinary = true;

                reqFTP.Credentials = new NetworkCredential(ftpUserID, ftpPassword);

 

                //Getting response from Request.

FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse();

 

                //Getting FileStream from the response.

                Stream ftpStream = response.GetResponseStream();

                //Deletes the File from the FTP path.

                DeleteFTPFile(response);

 

                ftpStream.Close();

                response.Close();

            }

            catch (Exception ex)

            {

                HttpContextClass.GetHttpResponse().Write(ex.Message);

            }

        }

 

        //Deletes file from server.

        public static void DeleteFTPFile(FtpWebResponse response)

        {

            try

            {

                string result = string.Empty;

                //Getting response form request.

                Stream datastream = response.GetResponseStream();

                //Reading the Stream to the end of file.

                StreamReader sr = new StreamReader(datastream);

                result = sr.ReadToEnd();

                sr.Close();

                datastream.Close();

                response.Close();

            }

            catch (Exception ex)

            {

                GetHttpResponse().Write(ex.Message + " Delete Error");

            }

        }

          //Create Request

        public static FtpWebRequest CreateFTPRequest(string fullFtpPath, string fileName)

        {

            return (FtpWebRequest)FtpWebRequest.Create(new Uri(fullFtpPath + "/" + fileName));

        }

        //Create Network Credential

        public static NetworkCredential CreateNetworkCredentials(string ftpUserID, string ftpPassword)

        {

            return new NetworkCredential(ftpUserID, ftpPassword);

        }

    }

}

Thanks for reading this article. Have a nice day.


Similar Articles