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)
{
}
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;

Join the conversation! Your thoughts help the community grow.