Differences

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

library_from_script 2015-12-21 library_from_script 2022-06-16 (current)
Line 5: Line 5:
Start by choosing a language. WinSCP .NET assembly can be used from any .NET language or any language that supports COM. Start by choosing a language. WinSCP .NET assembly can be used from any .NET language or any language that supports COM.
-If you do not have your own preferred language, use [[library_powershell|Windows PowerShell]].+If you do not have your own preferred language, use [[library_powershell|PowerShell]].
===== Mapping Script Commands to .NET Assembly Calls ===== ===== Mapping Script Commands to .NET Assembly Calls =====
Line 13: Line 13:
==== [[mode]] Batch Mode ==== ==== [[mode]] Batch Mode ====
-Batch scripts (specified using ''/script'' or ''/command'' [[commandline#scripting|command-line switches]]) run in an ''[[scriptcommand_option|option batch abort]]'' mode.+Batch scripts (specified using ''/script'' or ''/command'' [[commandline#scripting|command-line switches]]) run in an ''[[scriptcommand_option#batch|option batch abort]]'' mode.
To emulate the ''option batch abort'' call a method ''[[library_operationresultbase|OperationResultBase]].Check'' for all ''Session'' methods that return an operation result (such as ''[[library_session_getfiles|GetFiles]]'', ''[[library_session_putfiles|PutFiles]]'', and ''[[library_session_synchronizedirectories|SynchronizeDirectories]]''). To emulate the ''option batch abort'' call a method ''[[library_operationresultbase|OperationResultBase]].Check'' for all ''Session'' methods that return an operation result (such as ''[[library_session_getfiles|GetFiles]]'', ''[[library_session_putfiles|PutFiles]]'', and ''[[library_session_synchronizedirectories|SynchronizeDirectories]]'').
 +
 +If you are using ''option batch continue'' mode, handle ''[[library_session_queryreceived|Session.QueryReceived]]'' event and call ''[[ library_queryreceivedeventargs#continue|QueryReceivedEventArgs.Continue]]'' in the handler. //For an example, see [[library_example_recursive_download_custom_error_handling|*]].//
Read more about [[library_session#results|capturing errors]] in .NET assembly. Documentation for converting an individual scripting commands (such as ''get'' [[scriptcommand_get#net|command mapping]]) details more mapping specifics for the respective operations. Read more about [[library_session#results|capturing errors]] in .NET assembly. Documentation for converting an individual scripting commands (such as ''get'' [[scriptcommand_get#net|command mapping]]) details more mapping specifics for the respective operations.
==== [[default_config]] Default Configuration ==== ==== [[default_config]] Default Configuration ====
-Scripting mode by default [[scripting#configuration|shares configuration]] with graphical mode. On the contrary the .NET assembly is by default isolated from graphical mode configuration (equivalent to using ''/ini=nul'' [[commandline|command-line parameter]] in scripting mode).+Scripting mode by default [[scripting#configuration|shares configuration]] with graphical mode. On the contrary the .NET assembly is isolated from graphical mode configuration (equivalent to using ''[[commandline#configuration|/ini=nul]]'' command-line parameter in scripting mode).
It means that you cannot use [[session_configuration#site|stored sites]], when opening session with .NET assembly. You need to configure all your site settings directly in your code (using ''[[library_sessionoptions|SessionOptions]]'' class). It means that you cannot use [[session_configuration#site|stored sites]], when opening session with .NET assembly. You need to configure all your site settings directly in your code (using ''[[library_sessionoptions|SessionOptions]]'' class).
Line 30: Line 32:
In the scripting you can use paths relative to current working directory in parameters to script commands. In the scripting you can use paths relative to current working directory in parameters to script commands.
-In the .NET assembly, you need to always use absolute paths or paths relative to //initial// working directory (typically a home folder for remote path).+In the .NET assembly, you need to always use absolute paths or paths relative to //initial// working directory (for remote paths the //initial// directory is typically a home directory).
For example following script snippet: For example following script snippet:
Line 51: Line 53:
<code winscp> <code winscp>
# Connect # Connect
-open sftp://user:password@example.com/ -hostkey="ssh-rsa 2048 xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx"+open sftp://user:password@example.com/ -hostkey="ssh-rsa 2048 xxxxxxxxxxx..."
# Change remote directory # Change remote directory
cd /home/user cd /home/user
Line 73: Line 75:
    # Setup session options     # Setup session options
-    $sessionOptions = New-Object WinSCP.SessionOptions +    $sessionOptions = New-Object WinSCP.SessionOptions -Property @{ 
- ···$sessionOptions.Protocol = [WinSCP.Protocol]::Sftp +········Protocol = [WinSCP.Protocol]::Sftp 
-   $sessionOptions.HostName = "example.com" + ·······HostName = "example.com" 
-   $sessionOptions.UserName = "user" + ·······UserName = "user" 
-   $sessionOptions.Password = "password" + ·······Password = "password" 
-   $sessionOptions.SshHostKeyFingerprint = "ssh-rsa 2048 xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx"+ ·······SshHostKeyFingerprint = "ssh-rsa 2048 xxxxxxxxxxx..." 
 +    }
    $session = New-Object WinSCP.Session     $session = New-Object WinSCP.Session
Line 93: Line 96:
        # Download file to the local directory d:\         # Download file to the local directory d:\
        # Note use of absolute path         # Note use of absolute path
-        $transferResult = $session.GetFiles("/home/user/examplefile.txt", "d:\", $False, $transferOptions)+        $transferResult = 
 + ···········$session.GetFiles("/home/user/examplefile.txt", "d:\", $False, $transferOptions)
        # Throw on any error to emulate the default "option batch abort"         # Throw on any error to emulate the default "option batch abort"
Line 106: Line 110:
    exit 0     exit 0
} }
-catch [Exception]+catch
{ {
-    Write-Host $_.Exception.Message+    Write-Host "Error: $($_.Exception.Message)"
    exit 1     exit 1
} }
</code> </code>

Last modified: by martin