Differences

This shows you the differences between the selected revisions of the page.

2012-02-18 2012-02-18
c# heading and missing semicolon (martin) vbnet example (martin)
Line 35: Line 35:
===== Example ===== ===== Example =====
-==== C# Example ====+==== [[csharp]] C# Example ====
<code csharp> <code csharp>
using System; using System;
Line 128: Line 128:
</code> </code>
 +==== [[vbnet]] VB.NET Example ====
 +<code vbnet>
 +Imports System
 +Imports System.Globalization
 +Imports System.IO
 +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
 +                ' Connect
 +                session.Open(sessionOptions)
 +
 +                Dim stamp As String = DateTime.Now.ToString("yyyyMMdd", CultureInfo.InvariantCulture)
 +                Dim fileName As String = "export_" & stamp & ".txt"
 +                Dim remotePath As String = "/home/user/sysbatch/" & fileName
 +                Dim localPath As String = "d:\backup\" & fileName
 +
 +                ' Manual "remote to local" synchronization.
 +
 +                ' You can achieve the same using:
 +                ' session.SynchronizeDirectories(
 +                '    SynchronizationMode.Local, localPath, remotePath, false, false, SynchronizationCriteria.Time,
 +                '    new TransferOptions { IncludeMask = fileName }).Check();
 +                If session.FileExists(remotePath) Then
 +                    Dim download As Boolean
 +                    If Not File.Exists(localPath) Then
 +                        Console.WriteLine("File {0} exists, local backup {1} does not", remotePath, localPath)
 +                        download = True
 +                    Else
 +                        Dim remoteWriteTime As DateTime = session.GetFileInfo(remotePath).LastWriteTime
 +                        Dim localWriteTime As DateTime = File.GetLastWriteTime(localPath)
 +
 +                        If remoteWriteTime > localWriteTime Then
 +                            Console.WriteLine( _
 +                                "File {0} as well as local backup {1} exist, " & _
 +                                "but remote file is newer ({2}) than local backup ({3})", _
 +                                remotePath, localPath, remoteWriteTime, localWriteTime)
 +                            download = True
 +                        Else
 +                            Console.WriteLine( _
 +                                "File {0} as well as local backup {1} exist, " & _
 +                                "but remote file is not newer ({2}) than local backup local backup ({3})", _
 +                                remotePath, localPath, remoteWriteTime, localWriteTime)
 +                            download = False
 +                        End If
 +                    End If
 +
 +                    If download Then
 +                        ' Download the file and throw on any error
 +                        session.GetFiles(remotePath, localPath).Check
 +                        Console.WriteLine("Download to backup done.")
 +                    End If
 +                Else
 +                    Console.WriteLine("File {0} does not exist yet", remotePath)
 +                End If
 +            End Using
 +            Return 0
 +        Catch e As Exception
 +            Console.WriteLine("Error: {0}", e)
 +            Return 1
 +        End Try
 +
 +    End Function
 +
 +End Class
 +</code>

Last modified: by martin