public void RecursiveDeleteFTP(string path, bool RemFromLog, ref FtpConnection ftpcon) { ftpcon.SetCurrentDirectory(path); foreach (FtpFileInfo fi in ftpcon.GetFiles()) { string fpath = string.Format("{0}/{1}", path, fi.Name); ftpcon.RemoveFile(fpath); Log.Write("Gon'delete: {0}", fpath); if (RemFromLog) RemoveFromLog(fpath); } foreach (FtpDirectoryInfo di in ftpcon.GetDirectories()) { if (di.Name != "." && di.Name != "..") { string fpath = string.Format("{0}/{1}", noSlashes(path), di.Name); Log.Write("Gon'delete files in: {0}", fpath); RecursiveDeleteFTP(fpath, RemFromLog, ref ftpcon); } } ftpcon.RemoveDirectory(path); if (RemFromLog) RemoveFromLog(path); }
private static void RemoveDirectoryEvenIfNotEmpty(FtpConnection ftpConnection, string startDirectory) { if (!startDirectory.EndsWith("/")) startDirectory += "/"; List<string> directories = new List<string>(); directories.Add(startDirectory); int index = 0; while (index < directories.Count) { string currentDirectory = directories[index]; directories.AddRange(ftpConnection.GetDirectories(currentDirectory).Select(dir => currentDirectory + dir.Name + "/")); index++; } directories.Reverse(); List<string> fileNames = new List<string>(); foreach (var directory in directories) { foreach (var ftpFile in ftpConnection.GetFiles(directory)) { string fullPath = directory + ftpFile.Name; if (ftpConnection.FileExists(fullPath)) ftpConnection.RemoveFile(fullPath); } foreach (var ftpDir in ftpConnection.GetDirectories(directory)) { string fullPath = directory + ftpDir.Name; if (ftpConnection.DirectoryExists(fullPath)) ftpConnection.RemoveDirectory(fullPath); } } if (startDirectory != "./" && ftpConnection.DirectoryExists(startDirectory)) ftpConnection.RemoveDirectory(startDirectory); }