Differences
This shows you the differences between the selected revisions of the page.
2011-12-30 | 2012-01-10 | ||
&future_feature (martin) | toc + full example (martin) | ||
Line 40: | Line 40: | ||
Event ''[[library_session_filetransferred|Session.FileTransferred]]'' is raised for every uploaded or downloaded file. | Event ''[[library_session_filetransferred|Session.FileTransferred]]'' is raised for every uploaded or downloaded file. | ||
- | ~~NOTOC~~ | + | ===== Example ===== |
+ | <code csharp> | ||
+ | using System; | ||
+ | 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"; | ||
+ | |||
+ | Session session = new Session(); | ||
+ | |||
+ | // Will continuously report progress of synchronization | ||
+ | session.FileTransferred += FileTransferred; | ||
+ | |||
+ | // Connect | ||
+ | session.Open(sessionOptions); | ||
+ | |||
+ | SynchronizationResult synchronizationResult; | ||
+ | synchronizationResult = | ||
+ | session.SynchronizeDirectories( | ||
+ | SynchronizationMode.Remote, @"d:\www", "/home/martin/public_html", false); | ||
+ | |||
+ | // Throw on any error | ||
+ | synchronizationResult.Check(); | ||
+ | } | ||
+ | catch (Exception e) | ||
+ | { | ||
+ | Console.WriteLine("Error: {0}", e); | ||
+ | } | ||
+ | } | ||
+ | |||
+ | private static void FileTransferred(object sender, TransferEventArgs e) | ||
+ | { | ||
+ | if (e.Error == null) | ||
+ | { | ||
+ | Console.WriteLine("Upload of {0} succeeded", e.FileName); | ||
+ | } | ||
+ | else | ||
+ | { | ||
+ | Console.WriteLine("Upload of {0} failed: {1}", e.FileName, e.Error); | ||
+ | } | ||
+ | |||
+ | if (e.Chmod != null) | ||
+ | { | ||
+ | if (e.Chmod.Error == null) | ||
+ | { | ||
+ | Console.WriteLine("Permisions of {0} set to {1}", e.Chmod.FileName, e.Chmod.FilePermissions); | ||
+ | } | ||
+ | else | ||
+ | { | ||
+ | Console.WriteLine("Setting permissions of {0} failed: {1}", e.Chmod.FileName, e.Chmod.Error); | ||
+ | } | ||
+ | } | ||
+ | else | ||
+ | { | ||
+ | Console.WriteLine("Permissions of {0} kept with their defaults", e.Destination); | ||
+ | } | ||
+ | |||
+ | if (e.Touch != null) | ||
+ | { | ||
+ | if (e.Touch.Error == null) | ||
+ | { | ||
+ | Console.WriteLine("Timestamp of {0} set to {1}", e.Touch.FileName, e.Touch.LastWriteTime); | ||
+ | } | ||
+ | else | ||
+ | { | ||
+ | Console.WriteLine("Setting timestamp of {0} failed: {1}", e.Touch.FileName, e.Touch.Error); | ||
+ | } | ||
+ | } | ||
+ | else | ||
+ | { | ||
+ | // This should never happen with Session.SynchronizeDirectories | ||
+ | Console.WriteLine("Timestamp of {0} kept with its default (current time)", e.Destination); | ||
+ | } | ||
+ | } | ||
+ | } | ||
+ | </code> | ||