Using “Simple C# FTP Class” (see in linkografia) is possible delete recursively dir in ftp server.
class ftpClient
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Net; using System.IO; using System.Data; namespace Informaticapressapochista { public class ftpClient { private string host = null; private string user = null; private string pass = null; private FtpWebRequest ftpRequest = null; private FtpWebResponse ftpResponse = null; private Stream ftpStream = null; private int bufferSize = 2048; public ftpClient(string hostIP, string userName, string password) { host = hostIP; user = userName; pass = password; } public void DeleteFile(string deleteFile) { try { ftpRequest = (FtpWebRequest)WebRequest.Create(host + "/" + deleteFile); ftpRequest.Credentials = new NetworkCredential(user, pass); ftpRequest.UseBinary = true; ftpRequest.UsePassive = true; ftpRequest.KeepAlive = true; ftpRequest.Method = WebRequestMethods.Ftp.DeleteFile; ftpResponse = (FtpWebResponse)ftpRequest.GetResponse(); ftpResponse.Close(); ftpRequest = null; } catch (Exception ex) { throw ex; } return; } public string[] directoryListDetailed(string directory) { try { ftpRequest = (FtpWebRequest)FtpWebRequest.Create(host + "/" + directory); ftpRequest.Credentials = new NetworkCredential(user, pass); ftpRequest.UseBinary = true; ftpRequest.UsePassive = true; ftpRequest.KeepAlive = true; ftpRequest.Method = WebRequestMethods.Ftp.ListDirectoryDetails; ftpResponse = (FtpWebResponse)ftpRequest.GetResponse(); ftpStream = ftpResponse.GetResponseStream(); StreamReader ftpReader = new StreamReader(ftpStream); string directoryRaw = null; try { while (ftpReader.Peek() != -1) { directoryRaw += ftpReader.ReadLine() + "|"; } } catch (Exception ex) { Console.WriteLine(ex.ToString()); } ftpReader.Close(); ftpStream.Close(); ftpResponse.Close(); ftpRequest = null; try { if (directoryRaw == null) directoryRaw = string.Empty; string[] directoryList = directoryRaw.Split("|".ToCharArray()); return directoryList; } catch (Exception ex) { Console.WriteLine(ex.ToString()); } } catch (Exception ex) { Console.WriteLine(ex.ToString()); throw ex; } return new string[] { "" }; } public void deleteDirectory(string directoryName) { try { var direcotryChildren = directoryListSimple(directoryName); if (direcotryChildren.Any() && (!string.IsNullOrWhiteSpace(direcotryChildren[0]))) { foreach (var child in direcotryChildren) { if (child.Trim().Length > 0) { string mFileName = child; if (mFileName.Contains(@"/")) { string[] mFileNameNew = mFileName.Split('/'); mFileName = mFileNameNew[mFileNameNew.Length - 1]; } DeleteFile(directoryName + @"/" + mFileName); } } } ftpRequest = (FtpWebRequest)WebRequest.Create(host + "/" + directoryName); ftpRequest.Credentials = new NetworkCredential(user, pass); ftpRequest.UseBinary = true; ftpRequest.UsePassive = true; ftpRequest.KeepAlive = true; ftpRequest.Method = WebRequestMethods.Ftp.RemoveDirectory; ftpResponse = (FtpWebResponse)ftpRequest.GetResponse(); ftpResponse.Close(); ftpRequest = null; } catch (Exception ex) { throw ex; } return; } } .......... }
Main
... ftpClient m_ftpClient = new ftpClient(<host>, <user>, <password>); DataTable m_Dtbl = new DataTable(); m_Dtbl.Columns.Add("path", typeof(string)); m_Dtbl.Columns.Add("lenght", typeof(int)); string[] listadir=m_ftpClient.directoryListDetailed(<path to delete>); for (int i = 0; i < listadir.Length; i++) { string m_Item = listadir[i]; if (m_Item.Trim().Length > 0) { if (m_Item.Substring(0, 1) == "d") { string[] splittato = m_Item.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries); string m_nomedir = splittato[8]; DataRow m_NewRow = m_Dtbl.NewRow(); m_NewRow["path"] = @"/FTP_CAM/CAMSPOGL/" + splittato[8]; m_NewRow["lenght"] = ((string)m_NewRow["path"]).Length; m_Dtbl.Rows.Add(m_NewRow); } } } bool sentinel=true; while (sentinel) { sentinel = false; DataTable mDtblDupli = m_Dtbl.Copy(); foreach (DataRow m_DR in mDtblDupli.Rows) { string m_pathcorrente = (string)m_DR["path"]; string[] m_NewList = m_ftpClient.directoryListDetailed(m_pathcorrente); for (int i = 0; i < m_NewList.Length; i++) { string m_Item = m_NewList[i]; m_Item = m_Item.Trim(); if (m_Item.Length > 0) { string[] splittato = m_Item.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries); if (m_Item.Substring(0, 1) == "d") { if (m_Dtbl.Select("path like'"+m_pathcorrente+@"/"+splittato[8]+"'").Lenght == 0) { sentinel = true; DataRow m_NewRow = m_Dtbl.NewRow(); m_NewRow ["path"] = m_pathcorrente + @"/" + splittato[8]; m_NewRow ["lenght"] = ((string)NewRow ["path"]).Length; m_Dtbl.Rows.Add(NewRow); } } } } } } foreach (DataRow mDR in m_Dtbl.Select("","lenght DESC")) { m_ftpClient.deleteDirectory((string)mDR["path"]); } }
sorry if the solution is not very elegant……just remember that I still was a noob…….
Linkografia
Simple C# FTP Class – CodeProject