Search code examples
c#asp.netftpmonitoringfluentftp

Get last added/copied files from FTP Server using Fluent FTP in c#


I am using below code. (FluentFTP ) but Item.Modified will give the file created Date only. So its rendering based on the copied file created date. (Not when it copied) How will i get Files based on copied/Added Date in Fluent FTP.

private static void GetFiles()
    {
        using (FtpClient conn = new FtpClient())
        {
            string ftpPath = "ftp://myftp/";

            Dictionary<string, string> dirList = new Dictionary<string, string>();

            DateTime lastRunDate = DateTime.Now.AddMinutes(-2);

            string downloadFileName = @"C:\temp\FTPTest\";

            string newID = Guid.NewGuid().ToString();

            downloadFileName += newID + "\\";

            conn.Host = ftpPath;
            //conn.Credentials = new NetworkCredential("ftptest", "ftptest");
            conn.Connect();

            //Get all directories

            foreach (FtpListItem item in conn.GetListing(conn.GetWorkingDirectory(),
                FtpListOption.Modify | FtpListOption.Recursive))
            {
                // if this is a file
                if (item.Type == FtpFileSystemObjectType.File)
                {
                    if (item.Modified >= lastRunDate)
                    {
                        dirList.Add(item.FullName, item.Modified.ToString());
                        conn.DownloadFile(downloadFileName + item.FullName, item.FullName);
                    }
                }
            }
        }
    }

Solution

  • Explanation:

    I am downloading the files from FTP (Read permission reqd.) with same folder structure. So everytime the job runs I can check into the physical path same file(Full Path) exists or not If not exists then it can be consider as a new file. And i can do some action for the same and download as well.

    Its just an alternative solution.

    Code Changes:

    private static void GetFiles()
        {
            using (FtpClient conn = new FtpClient())
            {
                string ftpPath = "ftp://myftp/";
    
                string downloadFileName = @"C:\temp\FTPTest\";
    
                downloadFileName +=  "\\";
    
                conn.Host = ftpPath;
                //conn.Credentials = new NetworkCredential("ftptest", "ftptest");
                conn.Connect();
    
                //Get all directories
    
                foreach (FtpListItem item in conn.GetListing(conn.GetWorkingDirectory(),
                    FtpListOption.Modify | FtpListOption.Recursive))
                {
                    // if this is a file
                    if (item.Type == FtpFileSystemObjectType.File)
                    {
                        string localFilePath = downloadFileName + item.FullName;
    
                        //Only newly created files will be downloaded.
                        if (!File.Exists(localFilePath))
                        {
                            conn.DownloadFile(localFilePath, item.FullName);
                            //Do any action here.
                            Console.WriteLine(item.FullName);
                        }
                    }
                }
            }
        }