This is an old revision of the document!

Session.SynchronizeDirectories Method

Synchronizes directories.

This feature will be available only with the next release.

Advertisement

Syntax

public SynchronizationResult SynchronizeDirectories(
    SynchronizationMode mode,
    string localPath,
    string remotePath,
    bool removeFiles,
    bool mirror = false,
    SynchronizationCriteria criteria = SynchronizationCriteria.Time,
    TransferOptions options = null
)

Parameters

Name Description
SynchronizationMode mode Synchronization mode. Possible values are SynchronizationMode.Local, SynchronizationMode.Remote and SynchronizationMode.Both.
string localPath Full path to local directory.
string remotePath Full path to remote directory.
bool removeFiles When set to true, deletes obsolete files. Cannot be used for SynchronizationMode.Both.
bool mirror When set to true, synchronizes in mirror mode (synchronizes also older files). Cannot be used for SynchronizationMode.Both. Defaults to false.
SynchronizationCriteria criteria Comparison criteria. Possible values are SynchronizationCriteria.None, SynchronizationCriteria.Time (default), SynchronizationCriteria.Size and SynchronizationCriteria.Either. For SynchronizationMode.Both SynchronizationCriteria.Time can be used only.
TransferOptions options Transfer options. Defaults to null, what is equivalent to new TransferOptions().

Advertisement

Return Value

SynchronizationResult. See also Capturing results of operations.

Exceptions

Exception Condition
InvalidOperationException Session is not opened.
ArgumentException
ArgumentOutOfRangeException
Invalid combination of values of TransferOptions properties, SynchronizationMode, mirror or SynchronizationCriteria.
SessionLocalException Error communicating with winscp.com.
See the exception documentation for details.
TimeoutException Timeout waiting for winscp.com to respond.

Remarks

Event Session.FileTransferred is raised for every uploaded or downloaded file.

Example

using System;
using WinSCP;
 
class Test
{
    static void Main()
    {
        try
        {
            // Setup session options
            SessionOptions sessionOptions = new SessionOptions();
            sessionOptions.Protocol = Protocol.Sftp;
            sessionOptions.HostName = "example.com";
            sessionOptions.UserName = "user";
            sessionOptions.Password = "mypassword";
            sessionOptions.SshHostKey = "ssh-rsa 1024 xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx";
 
            Session session = new Session();
 
            // Will continuously report progress of synchronization
            session.FileTransferred += FileTransferred;
 
            // Connect
            session.Open(sessionOptions);
 
            SynchronizationResult synchronizationResult;
            synchronizationResult =
                session.SynchronizeDirectories(
                    SynchronizationMode.Remote, @"d:\www", "/home/martin/public_html", false);
 
            // Throw on any error
            synchronizationResult.Check();
        }
        catch (Exception e)
        {
            Console.WriteLine("Error: {0}", e);
        }
    }
 
    private static void FileTransferred(object sender, TransferEventArgs e)
    {
        if (e.Error == null)
        {
            Console.WriteLine("Upload of {0} succeeded", e.FileName);
        }
        else
        {
            Console.WriteLine("Upload of {0} failed: {1}", e.FileName, e.Error);
        }
 
        if (e.Chmod != null)
        {
            if (e.Chmod.Error == null)
            {
                Console.WriteLine("Permisions of {0} set to {1}", e.Chmod.FileName, e.Chmod.FilePermissions);
            }
            else
            {
                Console.WriteLine("Setting permissions of {0} failed: {1}", e.Chmod.FileName, e.Chmod.Error);
            }
        }
        else
        {
            Console.WriteLine("Permissions of {0} kept with their defaults", e.Destination);
        }
 
        if (e.Touch != null)
        {
            if (e.Touch.Error == null)
            {
                Console.WriteLine("Timestamp of {0} set to {1}", e.Touch.FileName, e.Touch.LastWriteTime);
            }
            else
            {
                Console.WriteLine("Setting timestamp of {0} failed: {1}", e.Touch.FileName, e.Touch.Error);
            }
        }
        else
        {
            // This should never happen with Session.SynchronizeDirectories
            Console.WriteLine("Timestamp of {0} kept with its default (current time)", e.Destination);
        }
    }
}

Advertisement

Last modified: by martin