Differences
This shows you the differences between the selected revisions of the page.
| 2012-01-10 | 2012-01-11 | ||
| toc (martin) | example (martin) | ||
| Line 33: | Line 33: | ||
| ===== Remarks ===== | ===== Remarks ===== | ||
| Event ''[[library_session_filetransferred|Session.FileTransferred]]'' is raised for every downloaded file. | Event ''[[library_session_filetransferred|Session.FileTransferred]]'' is raised for every downloaded file. | ||
| + | |||
| + | ===== Example ===== | ||
| + | <code csharp> | ||
| + | using System; | ||
| + | using System.Globalization; | ||
| + | using System.IO; | ||
| + | using WinSCP; | ||
| + | |||
| + | class Test | ||
| + | { | ||
| + | static void Main() | ||
| + | { | ||
| + | try | ||
| + | { | ||
| + | // Setup session options | ||
| + | SessionOptions sessionOptions = new SessionOptions(); | ||
| + | sessionOptions.Protocol = Protocol.Sftp; | ||
| + | sessionOptions.HostName = "example.com"; | ||
| + | sessionOptions.UserName = "user"; | ||
| + | sessionOptions.Password = "mypassword"; | ||
| + | sessionOptions.SshHostKey = "ssh-rsa 1024 xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx"; | ||
| + | |||
| + | using (Session session = new Session()) | ||
| + | { | ||
| + | // Connect | ||
| + | session.Open(sessionOptions); | ||
| + | |||
| + | string stamp = DateTime.Now.ToString("yyyyMMdd", CultureInfo.InvariantCulture); | ||
| + | string fileName = "export_" + stamp + ".txt"; | ||
| + | string remotePath = "/home/user/sysbatch/" + fileName; | ||
| + | string localPath = "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)) | ||
| + | { | ||
| + | bool download; | ||
| + | if (!File.Exists(localPath)) | ||
| + | { | ||
| + | Console.WriteLine("File {0} exists, local backup {1} does not", remotePath, localPath); | ||
| + | download = true; | ||
| + | } | ||
| + | else | ||
| + | { | ||
| + | DateTime remoteWriteTime = session.GetFileInfo(remotePath).LastWriteTime; | ||
| + | DateTime localWriteTime = File.GetLastWriteTime(localPath); | ||
| + | |||
| + | if (remoteWriteTime > localWriteTime) | ||
| + | { | ||
| + | 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; | ||
| + | } | ||
| + | } | ||
| + | |||
| + | if (download) | ||
| + | { | ||
| + | // Download the file and throw on any error | ||
| + | session.GetFiles(remotePath, localPath).Check(); | ||
| + | |||
| + | Console.WriteLine("Download to backup done.") | ||
| + | } | ||
| + | } | ||
| + | else | ||
| + | { | ||
| + | Console.WriteLine("File {0} does not exist yet", remotePath); | ||
| + | } | ||
| + | } | ||
| + | } | ||
| + | catch (Exception e) | ||
| + | { | ||
| + | Console.WriteLine("Error: {0}", e); | ||
| + | } | ||
| + | } | ||
| + | } | ||
| + | </code> | ||