Differences

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

2011-12-29 2011-12-29
RemovalArgs (martin) example (martin)
Line 28: Line 28:
| [[library_transferargs|TransferArgs]] | Provides data for ''[[library_session_filetransfer|Session.FileTransfer]]'' event. | | [[library_transferargs|TransferArgs]] | Provides data for ''[[library_session_filetransfer|Session.FileTransfer]]'' event. |
| [[library_transferoptions|TransferOptions]] | Defines options for file transfers. | | [[library_transferoptions|TransferOptions]] | Defines options for file transfers. |
 +
 +===== Example =====
 +<code csharp>
 +using WinSCP;
 +
 +class Test
 +{
 +    static void Main()
 +    {
 +        try
 +        {
 +            // Setup session options
 +            SessionOptions sessionOptions = new SessionOptions();
 +            sessionOptions.Protocol = SessionProtocols.Sftp;
 +            sessionOptions.HostName = "example.com";
 +            sessionOptions.UserName = "user";
 +            sessionOptions.Password = "mypassword";
 +            sessionOptions.HostKey = "ssh-rsa 1024 xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx";
 +
 +            // Connect
 +            Session session = new Session();
 +            session.Open(sessionOptions);
 +
 +            // Upload files
 +            TransferOptions transferOptions = new TransferOptions();
 +            transferOptions.TransferMode = TransferMode.Binary;
 +
 +            OperationResult<TransferArgs> transferResult;
 +            transferResult = session.PutFiles(@"d:\toupload\*", "/home/user/", false, transferOptions);
 +
 +            // Throw on any error
 +            transferResult.Check();
 +
 +            // Print results
 +            foreach (TransferArgs transfer in transferResult)
 +            {
 +                if (transfer.Error == null)
 +                {
 +                    Console.WriteLine("Upload of {0} succeeded", transfer.FileName);
 +                }
 +                else
 +                {
 +                    Console.WriteLine("Upload of {0} failed: {1}", transfer.FileName, transfer.Error);
 +                }
 +            }
 +        }
 +        catch(Exception e)
 +        {
 +            Console.WriteLine("Error: {0}", e);
 +        }
 +    }
 +}
 +</code>

Last modified: by martin