Search code examples
c#.netftpsynchronizationwinscp

Compare local and remote files on FTP with WinSCP lib


How i can check updates via WinSCP on FTP server with .NET?

I'm using SynchronizationMode.Local

For example:

if(have_new_update){
    MessageBox("U can update")
    if(ok){
        update();
    }
}else{
    return;
}

My code:

SessionOptions sessionOptions = new SessionOptions {
    Protocol = Protocol.Ftp, HostName = ftp_url, UserName = username, Password = pass
};
using (Session session = new Session()) {
    rtb_update_material.AppendText("\nConnected\n");
    //Transferring
    session.FileTransferred += FileTransferred;
    session.FileTransferProgress += SessionProgressBar;
    session.Open(sessionOptions);
    SynchronizationResult synchronizationResult;
    synchronizationResult = session.SynchronizeDirectories(SynchronizationMode.Local, @"MyPath\", "/", true);

    synchronizationResult.Check();
    if (synchronizationResult.IsSuccess)
        rtb_update_material.AppendText("Done\n");

Solution

  • Use Session.CompareDirectories to find the differences between a local and a remote directory:

    var diffs =
        session.CompareDirectories(SynchronizationMode.Remote, localPath, remotePath, false);
    
    if (diffs.Count > 0) 
    {
        // there are differences
    }