Here is the source code that will get all the folders & file recursively from FTP
Now you need to modify this code as per your requirement.
public static void GetFTPFilesPath(string ftpAddress, string UserName, string Password)
{
FtpWebRequest reqFTP = (FtpWebRequest)FtpWebRequest.Create(ftpAddress);
try
{
reqFTP.UsePassive = true;
reqFTP.UseBinary = true;
reqFTP.KeepAlive = false;
reqFTP.Credentials = new NetworkCredential(UserName, Password);
reqFTP.Method = WebRequestMethods.Ftp.ListDirectory;
FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse();
Stream responseStream = response.GetResponseStream();
List<string> files = new List<string>();
StreamReader reader = new StreamReader(responseStream);
while (!reader.EndOfStream)
files.Add(reader.ReadLine());
reader.Close();
responseStream.Dispose();
string ftpPath = string.Empty;
foreach (var fileName in files)
{
var parentDirectory = "";
PeriodicalFile lstFile = new PeriodicalFile();
if (fileName.Contains(".zip"))
{
ftpPath = ftpAddress + fileName;
}
else
{
parentDirectory += fileName + "/";
try
{
GetFTPFilesPath(ftpAddress + "/" + parentDirectory, UserName, Password);
}
catch (Exception ex)
{
ErrorLog.WriteLog("ftpFileProcessing", "GetFTPFilesPath(Else)", ex.Message, ex.StackTrace, "");
}
}
}
}
catch (Exception excpt)
{
reqFTP.Abort();
ErrorLog.WriteLog("ftpFileProcessing", "GetFTPFilesPath", excpt.Message, excpt.StackTrace, "");
}
}