1
  1. I have a local folder which contains files and directories (>2000 files).
  2. I uploaded this entire folder on my ftp.

Now for example let's say my ftp folder is called FTPFolder and my local folder is called LOCALFolder. These two folders are exactly the same for now.

And let's say the both folders contain a file called text.txt.

Now what I would like to do:

If I change the test.txt on the FTP, how could I detect it in C#? Getting all local files and all FTPfiles and then comparing them is just too long. Has anyone got another way of doing this ?

Basically the goal is to download all files on the FTP which are different from the same files but locally.

Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992
Keytrap
  • 421
  • 5
  • 14

2 Answers2

0

Here are possible options:

  1. Check the checksum - the best and fastest way, but it might not be present for the files in the first place.

  2. Check the size of the file and its timestamp. Not ideal, but it might work.

Other than that, I don't think there is anything you can and it's not a C# issue per se.

Vlad Stryapko
  • 1,035
  • 11
  • 29
0

Usual approach to synchronize local folder with FTP folder, is to compare file modification times.

Assuming that you are using FtpWebRequest .NET class, this is actually not trivial to implement. It does not have any standard way to retrieve file modification time.

See Retrieving creation date of file (FTP).

Even better would be to use a file checksum, but that's hardly possible.
See FTP: copy, check integrity and delete.


It would be way easier to use a 3rd party FTP client library that has a better support for retrieving the modification time. And even more easier, if you use FTP client library that supports a synchronization out of the box.

For example with WinSCP .NET assembly you can use Session.SynchronizeDirectories method:

// Setup session options
SessionOptions sessionOptions = new SessionOptions
{
    Protocol = Protocol.Ftp,
    HostName = "example.com",
    UserName = "user",
    Password = "mypassword",
};

using (Session session = new Session())
{
    // Connect
    session.Open(sessionOptions);

    // Synchronize files
    session.SynchronizeDirectories(
        SynchronizationMode.Local, @"C:\local\path", "/remote/path", false).Check();
}

WinSCP GUI can generate a code template for you.

(I'm the author of WinSCP)

Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992