Differences

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

2016-02-04 2016-03-07
hashtable style sessionoptions initilization (martin) symplifying C# example (martin)
Line 48: Line 48:
<code csharp> <code csharp>
using System; using System;
-using System.Globalization; 
-using System.IO; 
using WinSCP; using WinSCP;
Line 73: Line 71:
                session.Open(sessionOptions);                 session.Open(sessionOptions);
-                string stamp = DateTime.Now.ToString("yyyyMMdd", CultureInfo.InvariantCulture); +                // Download files 
-                string fileName = "export_" + stamp + ".txt"; +                TransferOptions transferOptions = new TransferOptions(); 
-                string remotePath = "/home/user/sysbatch/" + fileName; +                transferOptions.TransferMode = TransferMode.Binary; 
-                string localPath = "d:\\backup\\" + fileName; +  
- + ···············TransferOperationResult transferResult
-                // Manual "remote to local" synchronization. + ···············transferResult = session.PutFiles(&quot;/home/user/*", @"d:\download\", false, transferOptions); 
- +  
-                // You can achieve the same using: + ···············// Throw on any error 
-                // session.SynchronizeDirectories( + ···············transferResult.Check(); 
-                //    SynchronizationMode.Local, localPath, remotePath, false, false, SynchronizationCriteria.Time,  +  
-                //    new TransferOptions { FileMask = fileName }).Check(); + ···············// Print results 
-                if (session.FileExists(remotePath)) + ···············foreach (TransferEventArgs transfer in transferResult.Transfers)
-               { +
-                   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 &gt; 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, " + +
- ·······························&quot;but remote file is not newer ({2}) than 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);+                    Console.WriteLine("Download of {0} succeeded", transfer.FileName);
                }                 }
            }             }

Last modified: by martin