Differences
This shows you the differences between the selected revisions of the page.
| 2012-03-27 | 2012-03-28 | ||
| default not options is null (martin) | vb.net example (martin) | ||
| Line 52: | Line 52: | ||
| Event ''[[library_session_filetransferred|Session.FileTransferred]]'' is raised for every uploaded or downloaded file. | Event ''[[library_session_filetransferred|Session.FileTransferred]]'' is raised for every uploaded or downloaded file. | ||
| - | ===== Example ===== | + | ===== [[example]] Examples ===== |
| + | ==== C# Example ==== | ||
| <code csharp> | <code csharp> | ||
| using System; | using System; | ||
| Line 145: | Line 146: | ||
| </code> | </code> | ||
| + | ==== VB.NET Example ==== | ||
| + | <code vbnet> | ||
| + | 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" | ||
| + | .SshHostKey = "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 | ||
| + | </code> | ||