Differences
This shows you the differences between the selected revisions of the page.
| 2016-03-07 | 2016-03-07 | ||
| symplifying C# example (martin) | symplifying VB.NET example (martin) | ||
| Line 101: | Line 101: | ||
| ==== [[vbnet]] VB.NET Example ==== | ==== [[vbnet]] VB.NET Example ==== | ||
| <code vbnet> | <code vbnet> | ||
| - | Imports System.Globalization | ||
| - | Imports System.IO | ||
| Imports WinSCP | Imports WinSCP | ||
| Line 124: | Line 122: | ||
| session.Open(sessionOptions) | session.Open(sessionOptions) | ||
| - | Dim stamp As String = DateTime.Now.ToString("yyyyMMdd", CultureInfo.InvariantCulture) | + | ' Download files |
| - | Dim fileName As String = "export_" & stamp & ".txt" | + | Dim transferOptions As New TransferOptions |
| - | Dim remotePath As String = "/home/user/sysbatch/" & fileName | + | transferOptions.TransferMode = TransferMode.Binary |
| - | Dim localPath As String = "d:\backup\" & fileName | + | |
| - | + | ||
| - | ' Manual "remote to local" synchronization. | + | |
| - | ' You can achieve the same using: | + | Dim transferResult As TransferOperationResult |
| - | ' session.SynchronizeDirectories( _ | + | transferResult = session.GetFiles("/home/user/*", "d:\download\*", False, transferOptions) |
| - | ' SynchronizationMode.Local, localPath, remotePath, False, False, SynchronizationCriteria.Time, _ | + | |
| - | ' New TransferOptions With { .FileMask = fileName }).Check | + | ···············' Throw on any error |
| - | If session.FileExists(remotePath) Then | + | ···············transferResult.Check() |
| - | Dim download As Boolean | + | · |
| - | If Not File.Exists(localPath) Then | + | ···············' Print results |
| - | Console.WriteLine("File {0} exists, local backup {1} does not", remotePath, localPath) | + | ···············For Each transfer In transferResult.Transfers |
| - | download = True | + | Console.WriteLine("Download of {0} succeeded", transfer.FileName) |
| - | Else | + | Next |
| - | 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 | End Using | ||