This is an old revision of the document!
Session.SynchronizeDirectories Method
Advertisement
Syntax
C#
public SynchronizationResult SynchronizeDirectories( SynchronizationMode mode, string localPath, string remotePath, bool removeFiles, bool mirror = false, SynchronizationCriteria criteria = SynchronizationCriteria.Time, TransferOptions options = null )
VB.NET
Public Function SynchronizeDirectories( _ ByVal mode As SynchronizationMode, _ ByVal localPath As String, _ ByVal remotePath As String, _ ByVal removeFiles As Boolean, _ ByVal Optional mirror As Boolean = False, _ ByVal Optional criteria As SynchronizationCriteria = 1, _ ByVal Optional options As TransferOptions = Null _ ) As SynchronizationResult
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.
Examples
C# Example
using System; using WinSCP; class Example { public static int Main() { try { // Setup session options SessionOptions sessionOptions = new SessionOptions { Protocol = Protocol.Sftp, HostName = "example.com", UserName = "user", Password = "mypassword", SshHostKeyFingerprint = "ssh-rsa 1024 xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx" }; using (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(); } return 0; } catch (Exception e) { Console.WriteLine("Error: {0}", e); return 1; } } 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
VB.NET Example
Imports System Imports WinSCP Friend Class Example Public Shared Function Main() As Integer Try ' Setup session options Dim sessionOptions As New SessionOptions With sessionOptions .Protocol = Protocol.Sftp .HostName = "example.com" .UserName = "user" .Password = "mypassword" .SshHostKeyFingerprint = "ssh-rsa 1024 xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx" End with Using session As Session = New Session ' Will continuously report progress of synchronization AddHandler session.FileTransferred, AddressOf Example.FileTransferred ' Connect session.Open(sessionOptions) Dim synchronizationResult As SynchronizationResult synchronizationResult = _ session.SynchronizeDirectories( _ SynchronizationMode.Remote, "d:\www", "/home/martin/public_html", False) ' Throw on any error synchronizationResult.Check End Using Return 0 Catch e As Exception Console.WriteLine("Error: {0}", e) Return 1 End Try End Function Private Shared Sub FileTransferred(ByVal sender As Object, ByVal e As TransferEventArgs) If e.Error Is Nothing Then Console.WriteLine("Upload of {0} succeeded", e.FileName) Else Console.WriteLine("Upload of {0} failed: {1}", e.FileName, e.Error) End If If Not e.Chmod Is Nothing Then If e.Chmod.Error Is Nothing Then 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) End If Else Console.WriteLine("Permissions of {0} kept with their defaults", e.Destination) End If If Not e.Touch Is Nothing Then If e.Touch.Error Is Nothing Then 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) End If Else ' This should never happen with Session.SynchronizeDirectories Console.WriteLine("Timestamp of {0} kept with its default (current time)", e.Destination) End If End Sub End Class
Advertisement
PowerShell Example
Learn more about using WinSCP .NET assembly from PowerShell.
# Load WinSCP .NET assembly # Use "winscp.dll" for the releases before the latest beta version. [Reflection.Assembly]::LoadFrom("WinSCPnet.dll") | Out-Null # Session.FileTransferred event handler function FileTransferred { Param($e) if ($e.Error -eq $Null) { Write-Host ("Upload of {0} succeeded" -f $e.FileName) } else { Write-Host ("Upload of {0} failed: {1}" -f $e.FileName, $e.Error) } if ($e.Chmod -ne $Null) { if ($e.Chmod.Error -eq $Null) { Write-Host ("Permisions of {0} set to {1}" -f $e.Chmod.FileName, $e.Chmod.FilePermissions) } else { Write-Host ("Setting permissions of {0} failed: {1}" -f $e.Chmod.FileName, $e.Chmod.Error) } } else { Write-Host ("Permissions of {0} kept with their defaults" -f $e.Destination) } if ($e.Touch -ne $Null) { if ($e.Touch.Error -eq $Null) { Write-Host ("Timestamp of {0} set to {1}" -f $e.Touch.FileName, $e.Touch.LastWriteTime) } else { Write-Host ("Setting timestamp of {0} failed: {1}" -f $e.Touch.FileName, $e.Touch.Error) } } else { # This should never happen with Session.SynchronizeDirectories Write-Host ("Timestamp of {0} kept with its default (current time)" -f $e.Destination) } } # Main script try { $sessionOptions = New-Object WinSCP.SessionOptions $sessionOptions.Protocol = [WinSCP.Protocol]::Sftp $sessionOptions.HostName = "example.com" $sessionOptions.UserName = "user" $sessionOptions.Password = "mypassword" $sessionOptions.SshHostKeyFingerprint = "ssh-rsa 1024 xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx" $session = New-Object WinSCP.Session try { # Will continuously report progress of synchronization $session.add_FileTransferred( { FileTransferred($_) } ) # Connect $session.Open($sessionOptions) $synchronizationResult = $session.SynchronizeDirectories( [WinSCP.SynchronizationMode]::Remote, "d:\www", "/home/martin/public_html", $False) # Throw on any error $synchronizationResult.Check() } finally { # Disconnect, clean up $session.Dispose() } exit 0 } catch [Exception] { Write-Host $_.Exception.Message exit 1 }
Advertisement