Differences
This shows you the differences between the selected revisions of the page.
| 2012-03-28 | 2012-03-28 | ||
| system namespace is implicit (martin) | swapping powershell and vb.net examples (martin) | ||
| Line 136: | Line 136: | ||
| } | } | ||
| } | } | ||
| + | </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 With { .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 ({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> | </code> | ||
| Line 232: | Line 315: | ||
| exit 1 | exit 1 | ||
| } | } | ||
| - | </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 With { .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 ({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> | </code> | ||