Differences
This shows you the differences between the selected revisions of the page.
| 2012-09-24 | 2013-05-09 | ||
| 5.1 is stable (martin) | examples (martin) | ||
| Line 24: | Line 24: | ||
| | TimeoutException | Timeout waiting for ''winscp.com'' to respond. | | | TimeoutException | Timeout waiting for ''winscp.com'' to respond. | | ||
| - | ===== Example ===== | + | ===== [[example]] Examples ===== |
| - | See [[library_session_getfiles#example|example]] for ''[[library_session_getfiles#example|Session.GetFiles]]''. | + | ==== [[csharp]] C# Example ==== |
| + | <code csharp> | ||
| + | using System; | ||
| + | using WinSCP; | ||
| + | |||
| + | class Example | ||
| + | { | ||
| + | public static int Main() | ||
| + | { | ||
| + | try | ||
| + | { | ||
| + | // Setup session options | ||
| + | SessionOptions sessionOptions = new SessionOptions { | ||
| + | Protocol = Protocol.Sftp, | ||
| + | HostName = "example.com", | ||
| + | UserName = "user", | ||
| + | Password = "mypassword", | ||
| + | SshHostKeyFingerprint = "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); | ||
| + | |||
| + | const string remotePath = "/home/user/test.txt"; | ||
| + | if (session.FileExists(remotePath)) | ||
| + | { | ||
| + | Console.WriteLine("File {0} exists", remotePath); | ||
| + | // Now you can e.g. download file using session.GetFiles | ||
| + | |||
| + | return 0; | ||
| + | } | ||
| + | else | ||
| + | { | ||
| + | Console.WriteLine("File {0} does not exists", remotePath); | ||
| + | return 1; | ||
| + | } | ||
| + | } | ||
| + | } | ||
| + | catch (Exception e) | ||
| + | { | ||
| + | Console.WriteLine("Error: {0}", e); | ||
| + | return 2; | ||
| + | } | ||
| + | } | ||
| + | } | ||
| + | </code> | ||
| + | |||
| + | ==== [[vbnet]] VB.NET Example ==== | ||
| + | <code vbnet> | ||
| + | Imports System | ||
| + | Imports WinSCP | ||
| + | |||
| + | Friend Class Example | ||
| + | |||
| + | Public Shared Function Main() As Integer | ||
| + | |||
| + | Try | ||
| + | ············' Setup session options | ||
| + | Dim sessionOptions As New SessionOptions | ||
| + | With sessionOptions | ||
| + | .Protocol = Protocol.Sftp | ||
| + | .HostName = "example.com" | ||
| + | .UserName = "user" | ||
| + | .Password = "mypassword" | ||
| + | .SshHostKeyFingerprint = "ssh-rsa 1024 xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx" | ||
| + | End With | ||
| + | |||
| + | Using session As Session = New Session | ||
| + | ················' Connect | ||
| + | session.Open(sessionOptions) | ||
| + | |||
| + | Const remotePath As String = "/home/user/test.txt" | ||
| + | |||
| + | If session.FileExists(remotePath) Then | ||
| + | Console.WriteLine("File {0} exists", remotePath) | ||
| + | ' Now you can e.g. download file using session.GetFiles | ||
| + | |||
| + | Return 0 | ||
| + | Else | ||
| + | Console.WriteLine("File {0} does not exist", remotePath) | ||
| + | Return 1 | ||
| + | End If | ||
| + | End Using | ||
| + | |||
| + | Catch e As Exception | ||
| + | Console.WriteLine("Error: {0}", e) | ||
| + | Return 2 | ||
| + | End Try | ||
| + | |||
| + | End Function | ||
| + | |||
| + | End Class | ||
| + | </code> | ||
| + | |||
| + | ==== [[powershell]] PowerShell Example ==== | ||
| + | <code powershell> | ||
| + | try | ||
| + | { | ||
| + | ····# Load WinSCP .NET assembly | ||
| + | [Reflection.Assembly]::LoadFrom("WinSCP.dll") | Out-Null | ||
| + | |||
| + | # Setup session options | ||
| + | $sessionOptions = New-Object WinSCP.SessionOptions | ||
| + | $sessionOptions.Protocol = [WinSCP.Protocol]::Sftp | ||
| + | $sessionOptions.HostName = "example.com" | ||
| + | $sessionOptions.UserName = "user" | ||
| + | $sessionOptions.Password = "mypassword" | ||
| + | $sessionOptions.SshHostKeyFingerprint = "ssh-rsa 1024 xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx" | ||
| + | |||
| + | $session = New-Object WinSCP.Session | ||
| + | |||
| + | try | ||
| + | { | ||
| + | # Connect | ||
| + | $session.Open($sessionOptions) | ||
| + | |||
| + | $remotePath = "/home/user/test.txt" | ||
| + | |||
| + | if ($session.FileExists($remotePath)) | ||
| + | { | ||
| + | Write-Host("File {0} exists" -f $remotePath) | ||
| + | # Now you can e.g. download file using session.GetFiles | ||
| + | |||
| + | exit 0 | ||
| + | } | ||
| + | else | ||
| + | { | ||
| + | Write-Host ("File {0} does not exist" -f $remotePath) | ||
| + | exit 1 | ||
| + | } | ||
| + | } | ||
| + | finally | ||
| + | { | ||
| + | # Disconnect, clean up | ||
| + | $session.Dispose() | ||
| + | } | ||
| + | } | ||
| + | catch [Exception] | ||
| + | { | ||
| + | Write-Host $_.Exception.Message | ||
| + | exit 2 | ||
| + | } | ||
| + | </code> | ||
| + | |||
| + | ==== [[jscript]] JScript (WSH) Example ==== | ||
| + | In this example the JScript script is embedded into WSF file, to allow [[library_com_wsh#enums|access to enumeration values]]. | ||
| + | |||
| + | <code javascript> | ||
| + | <job> | ||
| + | <reference object="WinSCP.Session"/> | ||
| + | <script language="JScript"> | ||
| + | |||
| + | try | ||
| + | { | ||
| + | // Setup session options | ||
| + | var sessionOptions = WScript.CreateObject("WinSCP.SessionOptions"); | ||
| + | sessionOptions.Protocol = Protocol_Sftp; | ||
| + | sessionOptions.HostName = "example.com"; | ||
| + | sessionOptions.UserName = "user"; | ||
| + | sessionOptions.Password = "mypassword"; | ||
| + | sessionOptions.SshHostKeyFingerprint = "ssh-rsa 1024 xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx"; | ||
| + | |||
| + | var session = WScript.CreateObject("WinSCP.Session"); | ||
| + | |||
| + | var result; | ||
| + | |||
| + | try | ||
| + | { | ||
| + | // Connect | ||
| + | session.Open(sessionOptions); | ||
| + | |||
| + | var remotePath = "/home/user/test.txt"; | ||
| + | |||
| + | if (session.FileExists(remotePath)) | ||
| + | { | ||
| + | WScript.Echo("File " + remotePath + " exists"); | ||
| + | // Now you can e.g. download file using session.GetFiles | ||
| + | |||
| + | result = 0; | ||
| + | } | ||
| + | else | ||
| + | { | ||
| + | WScript.Echo("File " + remotePath + " does not exist"); | ||
| + | result = 1; | ||
| + | } | ||
| + | } | ||
| + | finally | ||
| + | { | ||
| + | // Disconnect, clean up | ||
| + | session.Dispose(); | ||
| + | } | ||
| + | |||
| + | WScript.Quit(result); | ||
| + | } | ||
| + | catch (e) | ||
| + | { | ||
| + | WScript.Echo("Error: " + e.message); | ||
| + | WScript.Quit(2); | ||
| + | } | ||
| + | |||
| + | </script> | ||
| + | </job> | ||
| + | </code> | ||
| + | |||
| + | |||
| + | ==== [[vbscript]] VBScript (WSH) Example ==== | ||
| + | In this example the VBScript script is embedded into WSF file, to allow [[library_com_wsh#enums|access to enumeration values]]. | ||
| + | |||
| + | <code vb> | ||
| + | <job> | ||
| + | <reference object="WinSCP.Session"/> | ||
| + | <script language="VBScript"> | ||
| + | |||
| + | Option Explicit | ||
| + | |||
| + | ' Setup session options | ||
| + | Dim sessionOptions | ||
| + | Set sessionOptions = WScript.CreateObject("WinSCP.SessionOptions") | ||
| + | With sessionOptions | ||
| + | .Protocol = Protocol_Sftp | ||
| + | .HostName = "example.com" | ||
| + | .UserName = "user" | ||
| + | .Password = "mypassword" | ||
| + | .SshHostKeyFingerprint = "ssh-rsa 1024 xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx" | ||
| + | End With | ||
| + | |||
| + | Dim session | ||
| + | Set session = WScript.CreateObject("WinSCP.Session") | ||
| + | |||
| + | ' Connect | ||
| + | session.Open sessionOptions | ||
| + | |||
| + | remotePath = "/home/user/test.txt" | ||
| + | |||
| + | Dim fs | ||
| + | Set fs = WScript.CreateObject("Scripting.FileSystemObject") | ||
| + | |||
| + | If session.FileExists(remotePath) Then | ||
| + | WScript.Echo "File " & remotePath & " exists" | ||
| + | ' Now you can e.g. download file using session.GetFiles | ||
| + | Else | ||
| + | WScript.Echo "File " & remotePath & " does not exist" | ||
| + | End If | ||
| + | |||
| + | ' Disconnect, clean up | ||
| + | session.Dispose | ||
| + | |||
| + | </script> | ||
| + | </job> | ||
| + | </code> | ||